Solved

Perl Sort not Working???

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Sort Question 4 133
remove duplicates from the csv file 13 108
Vb script to unzip a files and rename the files 12 103
Perl tar error 8 64
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…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

785 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