I think this catches all the same things as your code though the error message maybe different or less detailed:
sub range_to_list { my( $max, $range ) = @_; my( $lo, $sep, $hi ) = $range =~ m[^(\d+)?(?:(-)(\d*))?$] or die " +Invalid range:$range\n"; $lo ||= 0; $hi ||= $sep ? $max : $lo; $_ > $max and die "$_ out of range\n" for $lo, $hi; return $lo .. $hi; }
Output from your tests:
C:\test>junk61 maxind=10 rstr:x:...exc:Invalid range:x rstr:1-2-3:...exc:Invalid range:1-2-3 rstr:0:...ret:0: rstr:10:...ret:10: rstr:11:...exc:11 out of range rstr:4:...ret:4: rstr:4-6:...ret:4 5 6: rstr:7-:...ret:7 8 9 10: rstr:-2:...ret:0 1 2: rstr:-10:...ret:0 1 2 3 4 5 6 7 8 9 10: rstr:-11:...exc:11 out of range rstr:-0:...ret:0 1 2 3 4 5 6 7 8 9 10: rstr:10-:...ret:10: rstr:11-:...exc:11 out of range apples :one::two::three:
In reply to Re: Specifying a range of indices via the command line
by BrowserUk
in thread Specifying a range of indices via the command line
by eyepopslikeamosquito
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |