From yesterday's thread How do I sort a CSV file on multiple columns?, I thought you were using the Text::CSV or Text::CSV_XS module. In this code it looks like you're parsing it by hand.
The reason your sort does not work is because you join() the fields into one long string before pushing it onto the array. Then you try to sort on the separated fields that no longer exist. However even if you remove the join you'll run into problems because of all the quoting left in the fields.
Here's how you could be doing this using Text::CSV_XS:
#!/usr/bin/perl -w use strict; use warnings; use diagnostics; use Text::CSV_XS; my @rows; my $csv = Text::CSV_XS->new ( { binary => 1 } ) # should set binary a +ttribute. or die "Cannot use CSV: ".Text::CSV->error_diag (); my $expcol = 45; # Expect this many columns my $lineno = 0; while (<DATA>) { my $line = $_; chomp($line); $lineno++; #print "$lineno: $line\n"; if (! $csv->parse($line)) { warn "$lineno: skip due to parse error: ".$csv->input_error(); next; } my @columns = $csv->fields(); warn "$lineno: unexpected number of columns=".scalar(@columns).", +was expecting $expcol" if scalar(@columns) != $expcol; push (@rows, \@columns); } $lineno = 0; foreach my $row ( sort { $a->[0] cmp $b->[0] || $a->[44] <=> $b->[44] } @rows ) { $lineno++; print "$lineno: ".join(", ", @$row)."\n"; }
In reply to Re: Sorting help
by gmargo
in thread Sorting help
by mmittiga17
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |