Perl is helpful in expanding ranges automatically, even if they have alpha characters in them. The behavior of, say, 'a4' .. 'd8' follows Perl's behavior for the ++ operator. So the key is to make sure that if there are any alpha characters in the 'low', they should also exist in the 'high' end of the range. The other key is to make sure that single-component ranges get expanded to a xx .. xx form.

use strict; use warnings; use feature qw/say/; my $string = "ph1-3;25;t3;t47-t50;d4-6"; my @output = map { s/^([[:alpha:]]+)(\d+)-(\d+)/$1$2-$1$3/; my ( $low, $high ) = split /-/; $low .. $high || $low; } split /;/, $string; say "@output";

The output:

ph1 ph2 ph3 25 t3 t47 t48 t49 t50 d4 d5 d6

Which seems exactly what you were looking for.

Update: Out of curiosity I went looking on CPAN for something that could parse ranges. Parse::Range works, and while it produces the output we want, it also generates a 'non-numeric range' warning, which seems to not be a problem. In your case it works fine, but it wouldn't be a bad idea, if you were to use this solution, to look into the source and see if the module is going to behave nice for your specific needs.

use strict; use warnings; use feature qw/ say /; use Parse::Range qw/ parse_range /; say parse_range( $string );

The output:

non-numeric range: 'ph1-3;25;t3;t47-t50;d4-6' at C:/strawberry/perl/si +te/lib/Parse/Range.pm line 7. ph1 ph2 ph3 25 t3 t47 t48 t49 t50 d4 d5 d6

As you can see, if you ignore the warning, the output is correct.


Dave


In reply to Re: break-down ranges by davido
in thread break-down ranges by Anonymous Monk

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.