469,306 Members | 2,497 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

CSV File - Sort data by second field

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
Jul 27 '07 #1
7 9135
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
Jul 27 '07 #2
miller
1,089 Expert 1GB
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
Jul 27 '07 #3
numberwhun
3,503 Expert Mod 2GB
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
Jul 27 '07 #4
Great!...thanks a heap
Jul 27 '07 #5
KevinADC
4,059 Expert 2GB
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.
Jul 27 '07 #6
miller
1,089 Expert 1GB
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
Jul 27 '07 #7
KevinADC
4,059 Expert 2GB
It doesn't surprise me that there would be such modules. Any ones in particular you were thinking of?


- Miller
No, none in particular.
Jul 28 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Balaskas Evaggelos | last post: by
16 posts views Thread by Claudio Grondi | last post: by
9 posts views Thread by JimmyKoolPantz | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.