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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.