Consecutive Sorting

Posted on 2003-03-31
Medium Priority
Last Modified: 2010-03-05
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


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> ){
     close PROFILES;

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

     for( sort {$a<=>$b} keys %countriescount ){
          print COUNTRYOUTPUT "$_|$countriescount{$_}\n";
Question by:alexswebsite
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 84

Accepted Solution

ozo earned 500 total points
ID: 8237261
for( sort keys %countriescount ){
         print COUNTRYOUTPUT "$_|$countriescount{$_}\n";


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


Author Comment

ID: 8281909
Thank you.  Worked great, as usual!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question