CSV File - Sort data by second field | Member | | Join Date: Jul 2007
Posts: 40
| |
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 -
open (BASE, $dbk) || do {&no_open;};
-
@sorted = sort(<BASE>);
-
foreach $pair (@sorted) {
-
@show = split(/,/, $pair);
-
-
close(BASE);
-
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
| | | re: CSV File - Sort data by second field
I also tried this... -
open (BASE, $db) || do {&no_open;};
-
print BASE "$snumber,$kit,$id,$item_name,$in_out,$itemd_name,$date_out,$date_in,$name_out,$staff_out,$staff_in";
-
@sorted = <BASE>;
-
-
foreach $line (sort secondfield @sorted) {
-
@show = split(/,/,"$line");
-
sub secondfield {
-
($snumber,$kit,$id,$item_name,$in_out,$itemd_name,$date_out,$date_in,$name_out,$staff_out,$staff_in)= split(/,/,$a);
-
($snumber2,$kit2,$id2,$item_name2,$in_out2,$itemd_name2,$date_out2,$date_in2,$name_out2,$staff_out2,$staff_in2)= split(/,/,$b);
-
return ($kit2 <=> $kit);
-
}
-
$show[0] etc
-
-
close (BASE);
-
}
-
but it still sorts by the first field.
thanks
|  | Moderator | | Join Date: Oct 2006 Location: San Francisco, CA
Posts: 830
| | | 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 -
my @data;
-
open(BASE, $db) or die "Can't open $db: $!";
-
while (<BASE>) {
-
chomp;
-
push @data, [split ',', $_];
-
}
-
And then you can sort. The sort documentation itself will tell you how to do this. http://perldoc.perl.org/functions/sort.html -
my @sorted = sort {$a->[1] <=> $b->[1]} @data;
-
- Miller
|  | Site Moderator | | Join Date: May 2007 Location: New Hampshire
Posts: 2,567
| | | 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
| | | re: CSV File - Sort data by second field
Great!...thanks a heap
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | 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 -
my @data;
-
open(BASE, $db) or die "Can't open $db: $!";
-
while (<BASE>) {
-
chomp;
-
push @data, [split ',', $_]
-
}
-
And then you can sort. The sort documentation itself will tell you how to do this.
http://perldoc.perl.org/functions/sort.html -
my @sorted = sort {$a->[1] <=> $b->[1]} @data;
-
- 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.
|  | Moderator | | Join Date: Oct 2006 Location: San Francisco, CA
Posts: 830
| | | 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
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | 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.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,327 network members.
|