Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Consecutive Sorting

I have two consecutive functions that are very similar.  Both have a sort function, but only the first one works.  How can I make the second one sort too?  Also, how could I sort by element rather than by key?

Thanks, Alex



&buildAgeFile;
&buildCountriesFile;

sub buildAgeFile {

     open (PROFILES,"<$ProfileData") or die "can't open $ProfileData $!";
     while( <PROFILES> ){
          $agecount{$this_year - (split/\|/)[9]}++;
     }
     close PROFILES;

     open AGEOUTPUT,">$AgeStats" or die "Can't open $AgeStats $!";

     for( sort {$a<=>$b} keys %agecount ){
          print AGEOUTPUT "$_|$agecount{$_}\n";
     }
}

sub buildCountriesFile {

     open (PROFILES,"<$ProfileData") or die "can't open $ProfileData $!";
     while( <PROFILES> ){
          $countriescount{(split/\|/)[5]}++;
     }
     close PROFILES;

     open COUNTRYOUTPUT,">$CountriesStats" or die "Can't open $CountriesStats $!";

     for( sort {$a<=>$b} keys %countriescount ){
          print COUNTRYOUTPUT "$_|$countriescount{$_}\n";
     }
}
0
alexswebsite
Asked:
alexswebsite
1 Solution
 
ozoCommented:
for( sort keys %countriescount ){
         print COUNTRYOUTPUT "$_|$countriescount{$_}\n";
}

#or

for( sort {$countriescount{$a}<=>$countriescount{$b}} keys %countriescount ){
         print COUNTRYOUTPUT "$_|$countriescount{$_}\n";
}

 
0
 
alexswebsiteAuthor Commented:
Thank you.  Worked great, as usual!
Alex
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now