Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How do I sort a CSV file on multiple columns?

by kennethk (Abbot)
on Dec 09, 2009 at 18:42 UTC ( [id://811999]=note: print w/replies, xml ) Need Help??


in reply to How do I sort a CSV file on multiple columns?

Text::CSV is a module for writing and reading CSV data. If you want to sort the results, you have two choices in my mind (and I'm sure a host of others):
  1. You can slurp the file into some data structure - probably a list of lists (perllol) - and then sort the results, a la:

    #!/usr/bin/perl use strict; use warnings; my @lists = ([0,7,0], [1,3,0], [0,5,1], [1,2,1]); foreach my $list (sort {$a->[0] <=> $b->[0] or $a->[2] <=> $b->[2] } @ +lists) { print @$list, "\n"; }
  2. You can use a module that has the support you require. I like this idea better, using DBI and DBD::CSV. An example can be found at DBD::CSV.

Log In?
Username:
Password:

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

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

    No recent polls found