How to evaluate this string?

K

kaushik

Hi all,
I have a string like
$string='My marks are ( (10+20)*2) '
i should be able to convert this string to
$string='My marks are 60 '

the arithemaic expression may vary to any complexity. it may have
logical expressions like '&&' or '||' etc.

Can anyone help me out?

Thank you
Kaushik
 
T

Toni Erdmann

kaushik said:
Hi all,
I have a string like
$string='My marks are ( (10+20)*2) '
i should be able to convert this string to
$string='My marks are 60 '

the arithemaic expression may vary to any complexity. it may have
logical expressions like '&&' or '||' etc.

Can anyone help me out?

Thank you
Kaushik

$string =~ s/(\(.*\))/eval($1)/e;

converts everything that's between first ( and last )
to the result of it.

perldoc -f eval

for more information.

Toni
 
M

Matija Papec

X-Ftn-To: kaushik

kaushik said:
Hi all,
I have a string like
$string='My marks are ( (10+20)*2) '
i should be able to convert this string to
$string='My marks are 60 '

the arithemaic expression may vary to any complexity. it may have
logical expressions like '&&' or '||' etc.

#untested
#evaling everything inside ()
$string =~ s{ ( \(.*\) ) }{

#you might want to check $1 prior to evaling
#in case it contains malicious code..
my $val = eval $1;

#check perldoc perlvar for $@
$@ ? "($@)" : $val;
}xeg;
 
G

Gunnar Hjalmarsson

kaushik said:
Hi all,
I have a string like
$string='My marks are ( (10+20)*2) '
i should be able to convert this string to
$string='My marks are 60 '

the arithemaic expression may vary to any complexity. it may have
logical expressions like '&&' or '||' etc.

Can anyone help me out?

$string =~ s/(\(.+\))/$1/ee;

Please read

perldoc -f eval
perldoc -q "expand variables"
 
J

Josef Moellers

kaushik said:
Hi all,
I have a string like
$string='My marks are ( (10+20)*2) '
i should be able to convert this string to
$string='My marks are 60 '

the arithemaic expression may vary to any complexity. it may have
logical expressions like '&&' or '||' etc.

Can anyone help me out?

Can you help us?
What have you tried so far and where did it not work as expected?

This is not a machine where you put in a problem and out comes a
solution. You put in a problem and an attempt for a solution and out
comes a comment on your solution.

The solution will depend on how easy it is to detect the expression.
Once you can do that with a (Perl-) regular expression, the rest would
be easy.

Give it a try.
Hey, you might even learn from trying.
 
R

rajesh

Hi Gunnar Hjalmarsson ,
I am very very thankful to you.
your suggestion is reallly superb. even though it is quite simple ,
very difficult to get the idea. Again thanks a lot. I will ask if any
further queries in future and hope you continue the help.

Regards,
Rajesh
Kaushik
 
G

Gunnar Hjalmarsson

rajesh said:
I am very very thankful to you. your suggestion is reallly superb.
even though it is quite simple , very difficult to get the idea.

I'm glad you are thankful, but if I were you, I'd also take a look at
Matija's suggestion, which displays possible errors.
 
R

rajesh

Hi Hjalmarsson,
I have checked it .The error coming out is the
$val requires explicit package name. shall we add any other directive
if we use eval as matija used?

Thanks
Rajesh
 
G

Gunnar Hjalmarsson

[ Please provide some context when replying to a message. Most people,
who may be able to help, do _not_ access this Usenet group via Google. ]
I have checked it .

What have you checked? Oh, are you talking about Matija's code suggestion?
The error coming out is the $val requires explicit package name.

Matija's code works just fine for me. If it doesn't for you, please post
a short but complete program, that people can copy and run, and that
demonstrates the problem you are experiencing.

Just as is recommended in the posting guidelines for this group, btw:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top