Got it. This one’s in fact even shorter, heh.
sub collapse { my $str = join ', ', @_; $str =~ s/(\d+)(?:, ((??{$^N + 1})))+/$1-$^N/g; return $str; }But I just noticed it’s buggy without the \b assertions your initial attempt had:
sub collapse { my $str = join ', ', @_; $str =~ s/\b(\d+)(?:, ((??{$^N + 1})\b))+/$1-$^N/g; return $str; }PS.: it would be nice if
tags were permitted in comments…