#!/opt/perl/bin/perl -w
use strict;

sub expand_number_list {
    my @a;
    my $list = $_[0];
    return () if $list =~ /[^0-9,\s-]/;
    $list =~ s{
        (-?\d+)     # a number with a possible leading -
        -           # a hyphen
        (-?\d+)     # the second number in the #-# pair
        }
        {$1..$2}xg; # replace the - with .. 
                    # (but not the - in negative numbers)
    eval "\@a = ($list)";
    return @a;
}

sub format_number_list {
    my @list = @_;
#    @list = sort {$a <=> $b} @list;
    my $prev = shift @list;
    my $return = "$prev";
    foreach my $num (@list) {
        if ($num == ($prev + 1)) {
            # Sequential number, add to list
            if ($return =~ /-(-?\d+)$/ && ($1 == $prev)) {
                $return =~ s/$1$/$num/;
            }
            else {
                $return .= "-$num";
            }
        } else {
            $return .= ", $num";
        }
        $prev = $num;
    }
    return $return;
}
