in reply to Sorting file rows and copy them in an array
In particular, you need to enforce list context somehow if you want the matching to return the matched part. So, use something like
sort { ($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0] } sort { ($a =~ /\d+/g)[0] <=> ($b =~ /\d+/g)[0] } sort { (split /;/, $a)[0] <=> (split /;/, $b)[0] }
It might be nicer to use the Schwartzian Transform or Orcish Maneuvre to avoid repeated matching:
# ST my @sorted = map $_->[1], sort { $a->[0] <=> $b->[0] } map [ (split /;/)[0], $_], @lines; # OM use Syntax::Construct qw{ // }; my %cache; my @sorted = sort { $cache{$a} //= (split /;/, $a)[0] <=> $cache{$b} //= (split /;/, $b)[0] } @lines;
Or, if you don't mind some warnings about non-numeric values, just compare the strings numerically directly:
sort { $a <=> $b } @lines
(All code untested.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting file rows and copy them in an array
by savio (Initiate) on Dec 11, 2014 at 15:48 UTC | |
by choroba (Cardinal) on Dec 11, 2014 at 16:00 UTC | |
by Anonymous Monk on Dec 11, 2014 at 15:59 UTC |