Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: sorting CSV files

by derby (Abbot)
on Nov 30, 2005 at 14:36 UTC ( [id://512946]=note: print w/replies, xml ) Need Help??


in reply to sorting CSV files

TMTOWTDI ... but an array of arrays would work:

#!/usr/bin/perl my $sheet; my $count = -1; while( <DATA> ) { chomp; $count++; # skip header next unless $count; my $row; @$row = split( /,/, $_ ); push @$sheet, $row; } foreach my $row ( sort { $a->[1] <=> $b->[1] } @$sheet ) { print join( ',', @$row ), "\n"; } __DATA__ Name,Score,State Mike,67,CA Rob,63,FL Jim,72,IL Chan,32,AZ
-derby

Replies are listed 'Best First'.
Re^2: sorting CSV files
by argupta (Initiate) on Feb 05, 2009 at 22:43 UTC
    Hello derby, Im working in a similar data sctructure. In my case the csv files are around 26Mb. And I want to sort them with reference to a particular column, and then taking a sorted group, i want to sort them again using a different column. Looking at your earlier solution, I am unable to sort them in a hash table. (Since that is the best way I can think of)

      What hash? There's no hash there, just references to arrays. You can always do a secondary sort:

      foreach my $row ( sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] } +@$sheet ) {
      That construct will sort first by second col and then by the third col.

      -derby

Log In?
Username:
Password:

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

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

    No recent polls found