Perl Sort not Working???


Why won't the following work?

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

#!/share/bin/perl -w


    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};


Who is Participating?
jmcgConnect With a Mentor OwnerCommented:
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.)
TintinConnect With a Mentor Commented:
You don't populate the gdp hash, nor open the file referenced by the RESULTS filehandle.
WarkemAuthor Commented:
Am a perl novice how do I populate the gdp hash with the gdp data?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.