sub format_number_list {
   my ($prev, @list) = @_;
   my $result = $prev;
   my $range = 0;

   for my $cur (@list) {
     if ($cur == $prev + 1) {
       $range = 1;
     } elsif ($range) {
       $result .= "-$prev, $cur";
       $range = 0;
     } else {
       $result .= ", $cur";
     }
     $prev = $cur;
   }

   $result .= "-$prev" if $range;
   $result;
}


sub expand_number_list {
   return map {(m/(\d+)-(\d+)/)?($1..$2):$_} split /, /, shift;
}

format_number_list(1, 2);
