I tried parsing a common string depiction of ranges (e.g. 1-9
) into actual ranges (e.g. 1 .. 9
), but often got weird results when including two digit numbers. For example, 1-10
results in the single value 1
instead of a list of ten values and 11-20
gave me four values (11 10 21 20
), half of which aren't even in the expected numerical range:
put get_range_for('1-9');
put get_range_for('1-10');
put get_range_for('11-20');
sub get_range_for ( $string ) {
my ($start, $stop) = $string.split('-');
my @values = ($start .. $stop).flat;
return @values;
}
This prints:
1 2 3 4 5 6 7 8 9
1
11 10 21 20
Instead of the expected:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
(I figured this out before posting this question, so I have answered below. Feel free to add your own answer if you'd like to elaborate).