Solved

Perl Sort not Working???

Posted on 2004-04-04
4
193 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now