emilford has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking to sort a list of file names that my program has read in from a specific directory, based on information stored in each individual file. The files contain information on individual houses (location, price, beds, baths, etc). My program currently reads in all of the file names into an array and the traverses through the array printing out the appropriate HTML using the information found in the file. The problem is that I need to sort these file names by ascending order of price before printing each one out.

For example:

# the array of names would be similar to this
@array = ("file1", "file2", etc)

# each file would have house information on seperate lines
-price
-location
-beds
-etc

What I want to do is sort all of the file names in "array" by their price before "array" is traversed and the HTML code is generated.

Can someone help with a relatively simple solution to this problem?

Replies are listed 'Best First'.
Re: help sorting
by RMGir (Prior) on Mar 31, 2002 at 21:26 UTC
    Sure, load up your data into a hash-based structure (see ARRAY OF HASHES in perldsc).
    my @houses; # populate @houses, an AOH (see perldsc) foreach my $file(@array) { open HOUSE "<$file"; my $hr={input_file => $file}; # do whatever parsing you need to fill in the fields # this assumes that price, location, etc... are all one # line each in file $hr->{price} = <HOUSE>; chomp $hr->{price}; $hr->{location} = <HOUSE>; chomp $hr->{location}; #... close(HOUSE); push @houses, $hr; } # sort @houses=sort {$a->{price} <=> $b->{price}} @houses; #generate HTML
    Make sense?
    --
    Mike