in reply to File Sorting
Then sort on the 'z' columnopen(my $fh, "tempfile") or die("ack - $!"); chomp( my $header = <$fh> ); my(undef, @headers) = split ' ', $header; my(%cols) = do { my $i = 0; map { $_ => $i++ } @headers; }; my %info; while(<$fh>) { chomp; next if m< ^ \s* \z >x; my($name, @rows) = split; push @{ $info{$name} }, @rows; } close $fh;
Of course a much better idea would be to use one of the CSV type modules such as tilly's Text::xSV or Text::CSV_XS.open(my $fh, ">", "output") or die("ack - $!"); print $fh $header; print $fh "$_ @{ $info{$_} }\n" for reverse sort { $info{$a}->[ $cols{z} ] <=> $info{$b}->[ $cols{z} ] } keys %info; close $fh;
_________
broquaint
update: changed header regex => split, as I had a bad meme about multiple matches pushing onto the return list, unfortunately not e.g
my(@list) = "foo bar baz" =~ m< (?: (\w+) \s* )+ >x; print "@list"; __ouput__ baz # not - foo bar baz
update 2: posting on a Friday != a good idea
the previous example not working presumably because it was missing the g modifier, and the subsequent captures over-rode the previous one.my(@list) = "foo bar baz" =~ m< (\w+) [ ]? >xg; print "@list"; __output__ foo bar baz
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: File Sorting
by bronto (Priest) on Jul 19, 2002 at 16:36 UTC | |
|
Re: Re: File Sorting
by rchou2 (Novice) on Jul 19, 2002 at 18:37 UTC |