Вот один из возможных способов:
@numbers = (1,2,3,6,8,9,11,12,13,14,15,20);
@list = ();
$first = $last = shift @numbers;
foreach (@numbers,inf) {
if ($_ > $last+1) {
if ($first == $last) {
push @list, $first;
} elsif ($first+1 == $last) {
push @list, $first, $last;
} else {
push @list, "$first-$last";
}
$first = $_;
}
$last = $_;
}
print join ',', @list;