Solved

Perl Sort not Working???

Posted on 2004-04-04
4
198 Views
Last Modified: 2012-05-04
Hi,

Why won't the following work?

$cname is the name of a country i.e. "Zimbabwe"
$gdp is a number


#!/share/bin/perl -w

$DATA='Data.txt';


    open (DATA, "Data.txt") || die "couldn't open the file!";


    while (<DATA>)
    {
            ($cname,$capital,$area,$population,$life,$gdp) = split(',',$_);
                  
                            
           
    }
    foreach $cname (sort by_number keys %gdp) {
    print RESULTS "$cname : $gdp{$cname}\n";
    }
   
    sub by_number {
    $gdp{$b} <=> $gdp{$a};
    }

   


   
    close(DATA);
0
Comment
Question by:Warkem
4 Comments
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 35 total points
ID: 10754692
You don't populate the gdp hash, nor open the file referenced by the RESULTS filehandle.
0
 

Author Comment

by:Warkem
ID: 10754745
Am a perl novice how do I populate the gdp hash with the gdp data?

cheers
0
 
LVL 20

Accepted Solution

by:
jmcg earned 90 total points
ID: 10754839
Inside your loop, after the split, you have a $cname and a $gdp value that you want to be associated, using $cname as the key and $gdp as the value. You have to build this hash up country by country while reading the input data.

(We're treating this as if you were asking for help on a homework problem.)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 …
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…

791 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