Solved

Perl Sort not Working???

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

20 Experts available now in Live!

Get 1:1 Help Now