On 18/01/2012 05:04, Jeff Peng wrote:
于 2012-1-18 11:31, Chris Stinemetz 写道:
Would someone kindly advise me in sorting this array:

my @array = qw(c r v vr tr re c.p[1] c.p[3] c.p[2] c.p[4] c.p[7]
c.p[6] c.p[5] c.p[8] c.t[1] c.t[3] c.t[2]);

I only want to sort the elements that have a numeric value inside the
braces so that all the other elements have their original index
preserved.




I guess this is what you wanted.

my @new = map { $_->[0] }
sort { $a->[1]->[0] cmp $b->[1]->[0] or
$a->[1]->[1] <=> $b->[1]->[1] }
map {[$_,[ $_=~/\.(.+)\[(\d+)\]/ ]]}
@array;

The output:
c r v vr tr re c.p[1] c.p[2] c.p[3] c.p[4] c.p[5] c.p[6] c.p[7] c.p[8]
c.t[1] c.t[2] c.t[3]

Yes, but this needs a

  no warnings 'uninitialized';

to prevent it from shouting about the elements with no number in
brackets at the end.

Rob


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to