若你文字與數字是亂序的話,可以參考看看
use strict;
my $str = "Z[2], Z[1], C[1], C[0], D, P[2], P[1], C[2], B, P[3], A, T,";
my @str_split = split/, */,$str;
@str_split = sort{ (split/\[/,$a)[0] cmp (split/\[/,$b)[0] } @str_split;
my @letter;
my @letter_val;
for (@str_split){
if(/(\w+)\[([+-]*\d+)\]/){
if ($letter[$#letter] ne $1){
push @letter, $1 ;
push @{$letter_val[$#letter]}, $2;
}else{
push @{$letter_val[$#letter]}, $2;
}
}elsif(/\w+/){
push @letter, $_ ;
}
}
for (0..$#letter){
if (@letter_val[$_]){
my @sort_val = sort {$b <=> $a} @{$letter_val[$_]};
printf("[%d:%d] ",$sort_val[0],$sort_val[$#sort_val]);
}
print $letter[$_]."\n";
}