Connecting Tech Pros Worldwide Help | Site Map

CSV File - Sort data by second field

Member
 
Join Date: Jul 2007
Posts: 40
#1: Jul 27 '07
How do I sort the contents of a flat file based on the second field?
What I have now sorts by the first field - 0

Expand|Select|Wrap|Line Numbers
  1. open (BASE, $dbk) || do {&no_open;};
  2. @sorted = sort(<BASE>);
  3. foreach $pair (@sorted) {
  4.     @show = split(/,/, $pair);
  5.  
  6.     close(BASE);
  7.  
My fields are:
[0,1,2,3,4,5,6,7,8,9,10,11]

many thanks
Paul
Member
 
Join Date: Jul 2007
Posts: 40
#2: Jul 27 '07

re: CSV File - Sort data by second field


I also tried this...

Expand|Select|Wrap|Line Numbers
  1. open (BASE, $db) || do {&no_open;};
  2. print BASE "$snumber,$kit,$id,$item_name,$in_out,$itemd_name,$date_out,$date_in,$name_out,$staff_out,$staff_in";
  3. @sorted = <BASE>;
  4.  
  5. foreach $line (sort secondfield @sorted) {
  6.     @show = split(/,/,"$line");
  7.     sub secondfield {
  8.         ($snumber,$kit,$id,$item_name,$in_out,$itemd_name,$date_out,$date_in,$name_out,$staff_out,$staff_in)= split(/,/,$a);
  9.         ($snumber2,$kit2,$id2,$item_name2,$in_out2,$itemd_name2,$date_out2,$date_in2,$name_out2,$staff_out2,$staff_in2)= split(/,/,$b);
  10.         return ($kit2 <=> $kit);
  11.     }
  12.     $show[0] etc
  13.  
  14.     close (BASE);
  15. }
  16.  
but it still sorts by the first field.

thanks
miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#3: Jul 27 '07

re: CSV File - Sort data by second field


You must first parse the contents of your file so that perl can do the sorting on the field. There is no magic available here. You must program it all

Expand|Select|Wrap|Line Numbers
  1. my @data;
  2. open(BASE, $db) or die "Can't open $db: $!";
  3. while (<BASE>) {
  4.     chomp;
  5.     push @data, [split ',', $_];
  6. }
  7.  
And then you can sort. The sort documentation itself will tell you how to do this.

http://perldoc.perl.org/functions/sort.html

Expand|Select|Wrap|Line Numbers
  1. my @sorted = sort {$a->[1] <=> $b->[1]} @data;
  2.  
- Miller
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,567
#4: Jul 27 '07

re: CSV File - Sort data by second field


Its so funny, but that is exactly what I was thinking and working on for him, I just got caught up in work coding. Thanks for that confirmation Miller!

Jeff
Member
 
Join Date: Jul 2007
Posts: 40
#5: Jul 27 '07

re: CSV File - Sort data by second field


Great!...thanks a heap
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#6: Jul 27 '07

re: CSV File - Sort data by second field


Quote:

Originally Posted by miller

You must first parse the contents of your file so that perl can do the sorting on the field. There is no magic available here. You must program it all

Expand|Select|Wrap|Line Numbers
  1. my @data;
  2. open(BASE, $db) or die "Can't open $db: $!";
  3. while (<BASE>) {
  4.     chomp;
  5.     push @data, [split ',', $_]
  6. }
  7.  
And then you can sort. The sort documentation itself will tell you how to do this.

http://perldoc.perl.org/functions/sort.html

Expand|Select|Wrap|Line Numbers
  1. my @sorted = sort {$a->[1] <=> $b->[1]} @data;
  2.  
- Miller


There is a module or two that will handle the backend stuff of sorting mixed data in delimited fields. But really it seems so much easier to just write your own sorting function than to use a module unless a person just can't grasp how the sort() function works.
miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#7: Jul 28 '07

re: CSV File - Sort data by second field


It doesn't surprise me that there would be such modules. Any ones in particular you were thinking of?

I also considered using Text::CSV_XS just to demonstrate how one would, but it felt like overkill given the data as he expressed it. Must better to just focus the problem at sorting complex data structures.

- Miller
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#8: Jul 28 '07

re: CSV File - Sort data by second field


Quote:

Originally Posted by miller

It doesn't surprise me that there would be such modules. Any ones in particular you were thinking of?


- Miller

No, none in particular.
Reply