use 5.010; use strict; use warnings; # Read the string from input and remove the newline. my $list_string = <STDIN>; chomp $list_string; # Split it into a list on commas. # "1..5, 6, 7..10" becomes "1..5", "6", "7..10". my @list = split /\s*,\s*/, $list_string; # Go through each element checking for a range operator. # If there's a range operator, replace it with the range. # Otherwise leave it alone. @list = map { range_transform($_) } @list; print join ", ", @list; sub range_transform { my $string = shift; # Match the X..Y. If it doesn't match, just return it. return $string unless $string =~ m{^(\d+)\.\.(\d+)$}; # Perform the range operation. return $1..$2; }