my @ListIn  = qw(1 2 6 6 8 8 8 9 10 12 13 14 7 37 98);
my $Message;

$Message = &format_number_list(@ListIn);
print "\nList converts to [$Message]\n";

$Message = &expand_number_list($Message);
print "\nList converts to [$Message]\n";

############################################################################
# Sub        : format_number_list
# Parameters : a list of integers
# Purpose    : Converts a list of integers into a dash and comma separated
#              string that is easier to read
# Returns    : a dash and comma separated string
# Assumptions: Array has been validated for correctness before being passed
#              Duplicated numbers will be ignored
############################################################################
sub format_number_list
{
	my @ColArray   = (sort {$a<=>$b} @_);
# read in the array, and do a numerical sort
    my @ColumnList = ();
# list of formatted columns
    my $ColMsg     = "";
# column list as a single string

    my $min        = $ColArray[0];
    my $max        = $ColArray[0];
    my $newelt     = "";
	my $Column;
	my $idx;

    for($idx = 0; $idx <= $#ColArray; $idx++)
    {
        if($ColArray[$idx] == $min)
# first column - set the minimum of a possible range
        {
            $newelt = "$min,";
        }
        elsif( $ColArray[$idx] == ($ColArray[$idx - 1] + 1) )
# column number is one greater than last column,
        {
# so it must be part of a range
            $max = $ColArray[$idx] ;
            $newelt = "$min-$max,";
        }
        elsif( $ColArray[$idx] > ($ColArray[$idx - 1] + 1) )
# more than one greater - push the previous range
        {
# and start with a new minimum
            push @ColumnList,$newelt;

            $min = $ColArray[$idx];
            $max = $min;
            $newelt = "$min,";
        }
    }
    push @ColumnList,$newelt;
# don't forget to push the last range onto the array

    foreach $Column (@ColumnList)
# create the string for the message by appending
    {
# each range to the string
        $ColMsg .= "$Column";
    }

	$ColMsg =~ s/^(.*),$/$1/;
# clear the trailing comma

        $ColMsg =~ s/,/, /g;
    $Message = "$ColMsg";
	return $Message;
}

############################################################################
# Sub        : expand_number_list
# Parameters : a formatted list of integers
# Purpose    : Converts a formatted string of integers back into a plain list
# Returns    : a list of integers
# Assumptions: String has been validated for correctness before being passed
#              A correct string will have ranges with the starting number
#              smaller than the ending number, and a dash between them
############################################################################
sub expand_number_list
{
	my $formatted_string = shift;
# get the string
	my @WorkList         = ();
	my @NumberList       = ();
    my $ColMsg           = "";
# column list as a single string

	@WorkList = split/,/, $formatted_string;

	foreach $range (@WorkList)
	{
		my $min = "";
		my $max = "";

		if($range =~ /^(\d+)\s*-\s*(\d+)$/)
# it's a range only if there is a dash in the middle,
		{
#   with numbers on each end
			($min,$max) = ($range =~ /^(\d+)\s*-\s*(\d+)$/);
# grab only the begining and ending numbers
		}
		else
		{
			$min = $range;
# otherwise, it's just a number
		}

		if($max)
		{
			for($idx = $min; $idx <= $max; $idx++)
			{
				push(@NumberList,$idx);
			}
		}
		else
		{
			push(@NumberList,$range);
		}
	}

	foreach my $number (sort {$a<=>$b} @NumberList)
# sort numerically, and append to the output string
	{
		$ColMsg .= ",$number";
	}

	$ColMsg =~ s/^,(.*)$/$1/;
# clear the leading comma
	return $ColMsg;
}
