If you have Perl version 5.10+, the  (?|pattern) construct (see Extended Patterns in perlre) supports a neat approach:

c:\@Work\Perl>perl -wMstrict -le "use 5.010; ;; use Test::More 'no_plan'; use Test::NoWarnings; ;; my $rx_rng = qr{ \A (?| (\d*) - (\d+) | (\d+) - (\d*)) \z }xms; ;; my $max = 10; ;; for my $ar_vector ( [ '0-0', 0..0 ], [ '4-6', 4..6 ], [ '-2', 0..2 ], [ '3-', 3..$max ] +, ) { my ($rs, @rng) = @$ar_vector; is_deeply [ range($rs, $max, $rx_rng) ], \@rng, qq{'$rs'}; } ;; sub range { my ($rs, $r_max, $rx) = @_; ;; my ($lo, $hi) = $rs =~ $rx or die qq{not a range: '$rs'}; $lo = 0 unless length $lo; $hi = $r_max unless length $hi; ;; die qq{range inverted: '$rs'} if $lo > $hi; die qq{max out of range: '$rs'} if $hi > $r_max; ;; return $lo .. $hi; } " ok 1 - '0-0' ok 2 - '4-6' ok 3 - '-2' ok 4 - '3-' ok 5 - no warnings 1..5
I will leave you to test the exceptions.


Give a man a fish:  <%-(-(-(-<


In reply to Re: Specifying a range of indices via the command line by AnomalousMonk
in thread Specifying a range of indices via the command line by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.