
my $z = format_number_list(0, 17, 18, 99);

sub format_number_list {
     my @list = sort {$a <=> $b} @_;
     my $groups;
     my $g_n = 0; # group number starting at 0

     for my $n (@list) {
         (!$groups->[$g_n][-1] or ($groups->[$g_n][-1] == $n -1))?
             push(@{$groups->[$g_n]},$n):
             push(@{$groups->[++$g_n]},$n);
     }

     my @out;
     for my $group (@$groups) {
         if( @$group <= 2 ) {
             push(@out,@$group);
         } else {
             push(@out,join('-',$group->[0],$group->[-1]));
         }
     }
     return join(', ',@out);
}

sub expand_number_list {
     my $str = shift;
     $str =~ s/(\d+)-(\d+)/$1..$2/g;
     return map {$_} eval $str;
}
