$string = format_number_list (1, 2, 4, 5, 6, 7, 9, 13, 24, 25, 26, 27);
@list = expand_number_list($string);

print $string, "\n";
print "@list\n";

sub format_number_list {
    my @list = @_;
    my $string;
    my ($start, $end);
    my @pairs;

    $start = $end = $list[0];
    foreach my $n (1 .. @list) {
 if ($list[$n] == $list[$n - 1] + 1) {
     $end++;
 } else {
     push @pairs, ($start, $end);
     $start = $end = $list[$n];
 }
    }
    while (my ($s, $e) = splice(@pairs, 0, 2)) {
 $string .= ", " if $string;
 $string .= "$s";
 if ($s != $e) {
     $string .= "-" . $e;
 }
    }

    return $string;
}

sub expand_number_list {
    my $string = shift;
    my @list;

    my @parts = split /,/, $string;
    foreach my $p (@parts) {
        $p =~ s/^\s+//;
        $p =~ s/\s+$//;
        if ( $p =~ /(\d+)-(\d+)/) {
            foreach ($1 .. $2) {
                push @list, $_;
            }
        } else {
            push @list, $p;
        }
    }
    return @list;
}




