Tim Johnson wrote:
Just for the sake of showing another way to do it...
########################


my $input = "2-8,9,11,18-21";
$input =~ s/-/\.\./g;
print join(',',eval($input));

########################

or if you don't want to print out the list but iterate through it...

########################

my $input = "2-8,9,11,18-21";
$input =~ s/-/\.\./g;
foreach(eval($input)){
     ...do something
}

If you are accepting this string as input (from a user) then using eval is a major security risk unless you ensure that the string contains only valid data and you should probably test $@ to see if an error occurred. And you don't need to escape periods in a double quoted string.


my $input = '2-8,9,11,18-21';

$input =~ tr/0-9,-//cd;  # remove invalid characters from $input

$input =~ s/-/../g;
print join ',', eval $input;

die $@ if $@;  # eval reported error, then die



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to