This problem breaks neatly into two smaller ones. First, there is the problem of reading the CSV file. That problem may be (temporarily) simplified by assumimg the data has no embedded commas (or perhaps the file could be made to use tab or '~' or some other separator not part of the data).

Then the problem simplifies to a sorting one (as I read the problem statement). I.e., sort a two-dimensional array of data by the first three columns (however many other columns there are).

The simplest way to do this might then be this:

# comparison routine for sort() assumes (for fun) that col1 and col3 a +re # alpha, and col2 is numeric. Obviously this would have to reflect th +e data sub by_first_3_cols { $a->[0] cmp $b->[0] || # first, compare col1 $a->[1] <=> $b->[1] || # if equal, compare col2 $a->[2] cmp $b->[2]; # if equal, compare col3 } # sort list of arrayrefs, each containing data from one line of input my @sorted = sort by_first_3_cols map { chomp; my @columns = split /,/; [ @columns ] } <DATA>; # now print sorted list in CSV format (or whatever) print join( ',', @$_), "\n" for @sorted;

Let me acknowledge that the problem of extracting CSV data is more complex than simply splitting on ','. However, it may be made a simpler problem by specifying a different separator character--tab is my favorite, '|' (pipe) or '~' (tilde) work pertty well, too. Excel, for one thing, can write a tab-delimited file (having read the data from a comma-separated one).

This neatly end-runs the whole Text::CSV_XS issue, if possible, which IMHO is a good thing.

dmm


In reply to Re(2): Comma Delimited File by dmmiller2k
in thread Comma Delimited File by curtisb

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.