#!/usr/bin/perl -w use strict; sub list2ranges { my @list = sort {$a<=>$b} @_; my $current = $list[0]; my @ranges = ($current) ; for (my $i=1;$i <= @list; $i++){ if ($list[$i] && $list[$i] - $current == 1 || $list[$i] && $list[$i] - $current < 1 && substr($list[$i],0,-1) eq substr($current,0,-1) ){ $current = $list[$i]; } else{ $ranges[-1] .= " - $current" if $ranges[-1] != $current; $list[$i] && push @ranges, $current = "$list[$i]" ; } } return wantarray ? @ranges : join(", ",@ranges); } # Examples my @test = (qw( 01 2 3), 5, (7..9), (11..12), (17..19), 20, 30, qw(30.1 30.2 30.33 30.34 30.35)); my @ranged = list2ranges(@test); print join(", ",@test)," \n"; print join(", ",@ranged)," \n"; print scalar(list2ranges(@test)),"\n";