in reply to Generating ranges of numbers

I changed the input and output to avoid the issue mentioned by Anonymous Monk.

#! /usr/bin/perl use warnings; use strict; my @input = (1, 2, 3, 5, 6, 8, 10); my @ranges = ($input[0]); for my $i (1 .. $#input) { warn "\t@ranges"; if ($input[ $i - 1 ] + 1 == $input[$i]) { $ranges[-1] = (split /:/, $ranges[-1])[0] . ":$input[$i]"; } else { push @ranges, $input[$i]; } } use Test::More tests => 1; is_deeply \@ranges, [ '1:3', '5:6', '8', '10' ];

And, for the sake of TIMTOWTDI:

my @input = (1, 2, 3, 5, 6, 8, 10); my $last = $input[0]; my @ranges = map { ($_ > $#input || $input[$_] != 1 + $input[ $_ - 1 ]) ? do { my $s = $last . (":$input[ $_ - 1 ]") x ($last != $input[ +$_ - 1 ]); $last = $input[$_]; $s } : (); } 1 .. $#input + 1;

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,