in reply to sorting comma separated value file
I just coded this right off the top of my head, there may be errors. But you were looking for practice right? :)#!/usr/bin/perl use strict; my $col = $ARGV[1]; my $file = $ARGV[2]; $col--; my @data; open(IN,$file) || die "can't open: $!"; while(<IN>) { s/[\r\n]//g; push(@data,[ split(/,/,$_) ]); } close(IN); foreach (sort {$a->[$col] cmp $b->[$col]} @data) { print join(",",@{$_}),"\n"; }
Here's how this works. I take the column you want and the filename, decrement the column number (zero origin indexing), read in the file and make it a two dimensional array (or array or arrays, if you wish) then use sort (saying how to sort the elements) and print them back out.
I had slipped up and answered someone's homework once before, so I'm purposely being a little cryptic. That and I'm in a bit of a rush :). If you want a clearer explination, I'll come back and fill it later. But this should get you going in the right direction regardless.
/\/\averick
update:watch as Maverick makes silly typos! It's the CSV (comma seperated values) module not the CVS module which does something completely different altogether :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Re: sorting comma separated value file
by eduardo (Curate) on Aug 22, 2000 at 21:50 UTC | |
|
RE: Re: sorting comma separated value file
by Anonymous Monk on Aug 22, 2000 at 22:24 UTC |