in reply to Print series of numbers

You might like:

use strict; use warnings; my @list = sort {$a <=> $b} (1, 2, 3, 6, 9, 10, 13, 22, 20, 19, 15, 21 +); printf "%s\n", join ",", @list; my @newList; my $runLen = 0; for my $elt (@list, '') { if (! $runLen) { push @newList, $elt; $runLen = 1; next; } if ($elt eq '' || $runLen + $newList[-1] != $elt) { push @newList, "-", $runLen + $newList[-1] - 1 if $runLen != 1 +; push @newList, ',', $elt if $elt ne ''; $runLen = 1; } else { ++$runLen } } print @newList, "\n";

Prints:

1,2,3,6,9,10,13,15,19,20,21,22 1-3,6,9-10,13,15,19-22

True laziness is hard work