20/04/14 12:04:01 ZuF0W/s6.net
>>61 Perl5、正規表現を使った文字列処理ではなく、数値のリストに対する処理として書いてみた
sub f {
my @s;
while (my $e = shift) { push @s, $e }
@_ ? (@s ? (\@s, f(@_)) : f(@_)) : \@s;
}
sub g { @_ > 2 ? "$_[0]-$_[-1]" : @_ }
for ([1], [2,3], [1,2,3], [1,3], [1,3,4,5], [7,9]) {
@a = ();
@a[@$_] = @$_;
@b = f @a;
@c = map{g @$_} @b;
$"=',';
print "[@$_] -> '@c'\n";
}
実行結果
~ $ perl 17_61_resanc_0414fp.pl
[1] -> '1'
[2,3] -> '2,3'
[1,2,3] -> '1-3'
[1,3] -> '1,3'
[1,3,4,5] -> '1,3-5'
[7,9] -> '7,9'