> From: Jerry Preston <mailto:[EMAIL PROTECTED]> wrote:
>
> : I am looking for a simple Perl way to decode the
> : following, which can be any grouping of number
> :
> : 2-8,9,11,18-21
> :
> : Into
> : 2,3,4,5,6,7,8,9,11,18,19,20,21
> :
> : Any Ideas?
> :
> : Thanks for Your Time and Help,
>
> This same question seems to pop up every quarter.
> Is this for a course in perl or something?
I just did this, out of curiosity what are you doing with it? My method
handled all kinds of strings, but here's a starter for you:
$in = '2-8,9,11,18-21';
@inarray = split(',',$in);
@outarray = ();
foreach (@inarray) {
if (/^([\d.]+)-([\d.]+)$/) {
push @outarray, $1 .. $2;
}
else { push @outarray, $_; }
}
print join(",", @outarray), "\n";
Good luck. (If this was homework, you owe me!)
- B
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>