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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |