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

# Run functions
my @input = (1, 2, 4, 5, 6, 7, 9, 13, 24, 25, 26, 27);
my $format_out = format_number_list(@input);
my @expand_out = expand_number_list($format_out);

# Print results
my $orig_list = "";
my $expand_list = "";
for (@input) {
   $orig_list = ($orig_list eq "") ? $_ : "$orig_list, $_";
}
for (@expand_out) {
   $expand_list = ($expand_list eq "") ? $_ : "$expand_list, $_";
}
print "The original input to format_number_list was the
list:\n$orig_list\n\n";
print "The output string from format_number_list was the
string:\n$format_out\n\n";
print "Finally, the output list from expand_number_list was the
list:\n$expand_list\n\n";
print "Which shows the input == the output!\n" unless $orig_list ne
$expand_list;

sub format_number_list {
   my(@input_list) = @_;
   my @tmp_list = ();
   my $number;
   my $out_string = "";
   my $tmp_string = "";
   foreach $number (@input_list) {
      if ($#tmp_list == -1) {
         $tmp_list[0] = $number;
      } else {
         if ($number == ($tmp_list[$#tmp_list] + 1)) {
            $tmp_list[1] = $number;
         } else {
            $tmp_string = ($#tmp_list == 0) ? $tmp_list[0] : ($tmp_list[0] .
"-" . $tmp_list[1]);
            @tmp_list = ();
            $tmp_list[0] = $number;
            $out_string = ($out_string eq "") ? $tmp_string : ($out_string .
", $tmp_string");
         }
      }
   }
   $tmp_string = ($#tmp_list == 0) ? $tmp_list[0] : ($tmp_list[0] . "-" .
$tmp_list[1]);
   $out_string = $out_string . ", $tmp_string";
   return $out_string;
}

sub expand_number_list {
   my($input) = @_;
   my @input_list = split(/\s*\,\s*/,$input);
   my $number;
   my $first;
   my $last;
   my @out_list = ();
   foreach $number (@input_list) {
      if ($number =~ m/-/) {
         ($first,$last) = split(/-/,$number);
	 @out_list = (!(@out_list)) ? ($first .. $last) : (@out_list , ($first ..
$last));
      } else {
         @out_list = (!(@out_list)) ? ($number) : (@out_list , $number);
      }
   }
   return @out_list;
}

1;
