Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Sorting a CSV file

by rsiedl (Friar)
on Jul 17, 2006 at 03:39 UTC ( [id://561645]=note: print w/replies, xml ) Need Help??


in reply to Sorting a CSV file

Hi,

You might want to look at Text::CSV for parsing your csv file. It could make things a bit simpler.

And then, as McDarren says, Date::Calc would be quite helpful for your sort. Perhaps convert it to a unixtime format and then perform the sort.

A sort sub to get the job done:
# Store your values in a hash of hashes # $kols{line1} = { date => 12-12-06, bits => 12, ... } my $orderby = "date" # or "bits, ..." my $updown = "DESC" # or "ASC" foreach (sort { &mysort($updown) } keys %kols) { } # end-foreach sub mysort { my ($direction) = @_; if ($direction eq "DESC") { $kols{$b}{$orderby} <=> $kols{$a}{$orderby} || $kols{$b}{$orderby} cmp $kols{$a}{$orderby} } # end-if else { $kols{$a}{$orderby} <=> $kols{$b}{$orderby} || $kols{$a}{$orderby} cmp $kols{$b}{$orderby} } # end-else } # end-sub

Cheers.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-18 06:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found