in reply to FlatFile Sort Help
What you could do is grab the whole file into memory as an array of lines, sort it, and then split it. It will give the same effect as splitting then sorting on the first element unless the entire first element is the same in two or more lines.
my @lines = sort <LOOPFILE>; foreach ( @lines ) { # chomp; my @lineItems = split /\t/; # ... print OUTFILE "<br><b>$lineItems[0]</b> ($lineItems[1])\n"; # ... }
If you must sort after the split, you can always have an array of arrays. You can sort the outer array by the first element of each inner one.
my @lines = <LOOPFILE>; my @unsorted; foreach ( @lines ) { # chomp; my @lineItems = split /\t/; push @unsorted, \@lineItems; } my @sorted = sort { $a->[0] cmp $b->[0] } @unsorted; foreach ( @sorted ) { print OUTFILE '<br><b>', $_->[0], '</b> (', $_->[1], ")\n"; }
BTW, those probably are in need of a chomp() where noted, but they are commented out because I didn't see where you had been using them.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: FlatFile Sort Help
by Anonymous Monk on Nov 05, 2008 at 19:36 UTC |