in reply to Re^4: Sort by distance
in thread Sort by distance
I also still don't see the sort problem yet.while (<FILE>) { my($hStoreAdd $hStoreAdd2 $hStoreCity)=(split)[7,8,10]; #....add more vals above as you need, you can also use a range # of indicies as long as the range is lowest to highest, # [9,10,2..4] is ok but 4..2 would not be ok. }
UPDATE:
I looked back again this question.
It appears to me that what you have here is a loop that
loops on lines from some DB and you are able to print
them to some kind of HTML format.
The value distance in km is a derived value from the input data. So to get the printout to come out in a different order than the order provided by the DB in the loop, you need to save the data for each print, then sort that data structure, then print it.
You have a lot of values, less than probably you create in the split, but still a lot. So, one choice for the data structure is a LoH (list of hash), some folks would say AoH (Array of Hash). This allows you to have nice names that can be used later when doing the print.
Try something like this:
#!/usr/bin/perl -w use strict; my @km_testlist = qw( 120 500 300 400); my @LoH; #this will be a list of references to hash tables #now of course this just shows how to make and #sort this type of structure. #the stuff in this loop goes into your line process loop.. foreach my $thiskm (@km_testlist) #building a test structure { my %table = (); #don't need the =() but makes purpose very clea +r. $table{'val1'}="something"; #this data doesn't matter $table{'val2'}="aaaa"; $table{'km'}=$thiskm; push(@LoH,\%table); #pushes a ref to table hash onto @LoH } @LoH = sort { my $a_km = $a->{'km'}; #don't need the $a or $b_km temp my $b_km = $b->{'km'}; #trying to be clear for you $a_km <=> $b_km }@LoH; #so now instead of using the $vars, from each line, you use #the values in the hash, instead of 'val1', I would give the #sort of name you are already using. foreach my $tab_hashref (@LoH) { print "$tab_hashref->{'km'} $tab_hashref->{'val1'} $tab_hashref->{' +val2'}\n"; } #prints: #120 something aaaa #300 something aaaa #400 something aaaa #500 something aaaa
|
|---|