in reply to Re: Comma Delimited File
in thread Comma Delimited File
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
|
|---|