Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: sorting CSV files

by crashtest (Curate)
on Dec 01, 2005 at 02:04 UTC ( [id://513171]=note: print w/replies, xml ) Need Help??


in reply to sorting CSV files

Am I the only one this reeks of homework to? In general, people are happy to help if you at least start with some code and specific questions about it (and why it's not working).

Maybe you can turn in the following one liner (for Windows)...?

C:\temp>perl -F, -ane "BEGIN{$c = shift} push @a, [@F]; END{print join +(',' => @$_) for sort {($a->[abs($c)-1] cmp $b->[abs($c)-1]) * $c} (@ +a)};" -- -3 grades.csv Jim,72,IL Rob,63,FL Mike,67,CA Chan,32,AZ
... where the only argument (before the input file) specifies on which column to sort and in which direction (negative column value for descending sort, positive for ascending).

Replies are listed 'Best First'.
Re^2: sorting CSV files
by Anonymous Monk on Apr 28, 2008 at 07:19 UTC
    Hi, Thanks for wonderful one liner file sorting. Can you please explain this in detail that how it works ?

      I can, but I won't. The OP looked like a homework assignment. My goal was to post a working solution that was as cryptic, compressed, unreadable and definitely un-turn-in-able as possible.

      If you are genuinely interested, I would recommend reading:

      • perlrun for an explanation of -a -F, and -n.
      • perlmod to understand what BEGIN and END blocks do.
      • perlfunc for an explanation of the sort function.

        Thanks. :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-25 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found