in reply to Re^4: 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?
Ok, so I took this program and and that sample input.txt, ran it, and it worked. Now I do need to levels of sort, first descending numerical and then within the equal numbers, ascending alphabetically.
I've started reading on how to use map and am a little confused, but feel like I'm close.
In this section I know that I have to declare the first character in the string such that I can then sort by it, yes? This doesn't work, but it's something close?
sub handle_section { my ($fh) = @_; my ( @entries, $line ); while ( $line = <$fh> ) { last unless $line =~ m{ ( [A-Za-z] ) # 1 first character ( [^(]+ ) # 2 anything except opening paren \s # space \( # opening paren ( \d+ ) # 3 number }x; push @entries, [ pack( 'Na*', $2, $3, $1 ), $line ]; } print map { $_->[2] } sort { $b->[0] cmp $a->[0] || $a->[1] cmp $b->[1] } @entries; return $line; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Use Perl's Sort to only sort certain lines in a file?
by Anonymous Monk on Jan 02, 2015 at 03:40 UTC | |
by grahambuck (Acolyte) on Jan 02, 2015 at 06:03 UTC | |
by Anonymous Monk on Jan 02, 2015 at 07:36 UTC |