Solved

Perl Sort not Working???

Posted on 2004-04-04
4
199 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Remove Malware code from PHP file 6 100
Perl strange behaviour 5 74
Perl script to parse xml file 2 132
Perl tar error 8 70
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

730 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