in reply to Generating ranges of numbers
#!usr/bin/perl use strict; use warnings; my @array = ("1","2","3","5","6","7","9"); my @working = @array; #copy to "consume" my $start = shift @working; my $current= $start; my @output; while (my $next = shift @working) { if ($next == $current+1) { $current = $next; } else { got_result(); $start = $current = $next; } } got_result(); print join ",",@output; #prints: 1:3,5:7,9 sub got_result { if ($start == $current ) { push @output, "$start"; } else { push @output, "$start:$current"; } }
|
|---|