in reply to Re^5: Use Perl's Sort to only sort certain lines in a file?
in thread Use Perl's Sort to only sort certain lines in a file?
You'll probably want to read about Schwartzian transform. Anyway, perhaps it will be clearer without pack
Our @entries is an array of arrays. Each entry (inner array) has:last unless $line =~ m{ ( [^(]+ ) # 1 anything except opening paren \s # space \( # opening paren ( \d+ ) # 2 number }x; push @entries, [ $2, $1, $line ]; ... print map { $_->[2] } sort { $a->[0] <=> $b->[0] # sort by $2 || # or if equal $a->[1] cmp $b->[1] # sort by $1 } @entries;
So, we first sort by '1', then by 'wrestling'. Or we can pack '1' and 'wrestling' in just one string and sort by that... that's just a neat trick :)
map simply extracts the line from entry.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Use Perl's Sort to only sort certain lines in a file?
by grahambuck (Acolyte) on Jan 02, 2015 at 06:03 UTC | |
by Anonymous Monk on Jan 02, 2015 at 07:36 UTC |