in reply to Perl Module for dealing with number ranges
I certainly do not advocate doing it this way. Use one of the modules and reap the benefits of testing, a user base, docs you don't have to write, and future improvements. This is just for fun and because I found dmmiller2k's example a bit hard to read.
while (<DATA>) { chomp; print join(" + ", expand($_)), $/; } sub expand { my $input = shift; $input =~ s/[^-\d,]+//g; $input =~ s/-/../g; my ( %tmp, @tmp ); @tmp = ( eval "$input" ); @tmp{@tmp} = (1) x @tmp; # dedupe return sort { $a <=> $b } keys %tmp; } __DATA__ 1,7-10,3-5,15 or even ... my $input = "1,7-10,3-5,15";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2:dealing with coordinate ranges
by Discipulus (Canon) on May 20, 2014 at 11:27 UTC |