Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Sorting a File which contains numbers

by SireeshaYalamanchili (Initiate)
on Oct 30, 2006 at 13:05 UTC ( [id://581276]=perlquestion: print w/replies, xml ) Need Help??

SireeshaYalamanchili has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Sorting a File which contains numbers
by ww (Archbishop) on Oct 30, 2006 at 14:10 UTC
    SY...

    Expanding on madbombX 's cautionary observation, above: the Monastery is a treasury of knowledge and helpful educators but it is NOT a shop where, on request, we write code to satisfy your specific needs and for free, at that. Many if not most of us make our living writing code and while we do try to offer -- freely -- advice and information, your blunt request that we provide -- for free -- a particular script in a specified number of lines is (perhaps unintentionally?) offensive.

    You may also profit from noting this frequently offered admonition: "Show some effort; show us what you've tried!"

Re: Sorting a File which contains numbers
by madbombX (Hermit) on Oct 30, 2006 at 13:19 UTC
    Like a lot of monks, I am not one to completley write code for someone. Especially since it sounds like an assignment (since you are specifying the exact number of lines you need it in). However, to open the file up, check out IO::File, read in the line, put the vars into an array and then sort them:
    for my $line (<FH>) { my @unsorted = split / /, $line; # sort ascending my @sorted = sort @unsorted; # sort descending my @desc_sort = reverse @sorted; }
    Check out the pages on sort, reverse, and split for more information. There are certainly shorter ways to do this, but you need to understand what's going on before you can condense it.
Re: Sorting a File which contains numbers
by Hofmator (Curate) on Oct 30, 2006 at 14:11 UTC
    Watch out: sort sorts normally "in standard string comparison order" which is NOT numerically. In order to sort numerically, you have to use an explicit sort routine.

    With your expamle input file, this won't make a difference though, as it sorts the same numerically AND alphabetically.

    -- Hofmator

    Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Sorting a File which contains numbers
by swampyankee (Parson) on Oct 30, 2006 at 21:59 UTC

    If you're on *ix, use the system sort utility (sort -n). If you're on Windows, its sort command doesn't seem to have a numerical option (see sorting in XP).

    As an aside, you'll likely get much better response from the Monks if you, first, show what you've tried (in this case, seemingly, nothing), and, second, don't ask for code.

    Oh, and a hint:

    @out = sort { $a <=> $b } @in; # to sort array @in numerically, and pu +t it into @out.

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://581276]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2024-04-25 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found