in reply to Print series of numbers

I did not want to use neither min nor max, I just used reduce :)

So, please excuse my lengthy and bloated 13-line solution to this problem:

use List::AllUtils qw/reduce/; my @list = sort {$a <=> $b} (1, 2, 3, 6, 9, 10, 13, 22, 20, 19, 15, 21 +); print reduce { if(my ($sign,$last) = $a =~ /([,-]?)(\d+)$/){ if( $last+1 == $b ) { $a =~ s/[,-]?\d+$// if($sign eq '-'); $a.= "-$b"; } else { $a.=",$b"; }; }; $a; }@list;

The advantage of using reduce is that it allows you to look behind and see what you did before (for example,see the previous number you appended to the string).

The if conditions asks if the number is the next consecutive one so it can replace the high bound of the interval with a newer one,if not it just adds the new number to the list with comma separator.