On Sat, Dec 20, 2008 at 01:04, hotkitty <stpra...@gmail.com> wrote: > Hi, > > I have a variable for which I ultimately want to substitute w/ some > math. Is there a way to get the result "6"? > > my $number=123; > $number =~s/123/1+2+3/s; #This method results in "1+2+3", not the "6" > I am looking for > print "$number"; snip
It sounds like you are looking for the e option on the regex, but a regex is really not the right answer to your problem. Take a look at split instead: #!/usr/bin/perl use warnings; use strict; use List::Util qw/sum/; my $number = 123; $number =~ s/([0-9])([0-9])([0-9])/$1+$2+$3/ge; print "$number\n"; my $other = 12345; $other = sum(split //, $other); print "$other\n"; -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/