string formulas to integers

N

nancy.iida

I am trying to parse strings containing both numbers and operator
characters into an integer. I'm having some problems doing this.

Here's my program:

#!/usr/local/bin/perl
$number = 15 + 0.5 * 30; # this expression evaluates correctly to 30
$d2 = "15+0.5*30"; # this is the string I want to evaluate
$intd2 = int $d2; # will int create the value I want?
$floatd2 = sprintf "%f",$d2; # will sprintf to a float give me what I
want?
printf "number = $number\n";
printf "d2 = $d2\n"
printf "intd2 = $intd2\n"
printf "floatd2 = $floatd2\n";


Here's the resultant output:
number = 30
d2 = 15+0.5*30
intd2 = 15
floatd2 = 15

I was hoping to get the operators evaluated in the string expression?
Is there anyway of doing this short of parsing out the operators and
setting up the math? Am I just doing something wrong here?
Any help appreciated.
 
P

phaylon

nancy.iida said:
I was hoping to get the operators evaluated in the string expression? Is
there anyway of doing this short of parsing out the operators and setting
up the math?

This is Perl, so there are more possibilities. You could have a look at
'perldoc -f eval', or if you want it "more complicated" there's also
Parse::RecDescent, which may work for this. Of Course, if you just want to
parse those math expressions, it would be overhead.
Am I just doing something wrong here? Any help appreciated.

Beware of unchecked User-Input (If you only want to allow mathematical
expressions, it should be easy to limit the chars and make a regex to
validate), that's the first thing that comes into my head, when I think of
eval. Besides, "wrong" is (IMO) always depending on what you're trying to
do, and who you're asking.


p
 
N

nancy.iida

Thanks, Bernard...I had read the docs on eval, and I had interpreted it
as a trapping function and not seen it's other function as evaluating
the expression as a small perl program.
Worked great. Thanks for your help.
 
P

Paul Lalli

I am trying to parse strings containing both numbers and operator
characters into an integer.

Parsing means to dissect a string to get at its component parts. What
you are trying to do is evaluate a mathematical formula *without*
parsing it.
I'm having some problems doing this.

Here's my program:

#!/usr/local/bin/perl

you forgot
use strict;
and
use warnings;

$number = 15 + 0.5 * 30; # this expression evaluates correctly to 30
$d2 = "15+0.5*30"; # this is the string I want to evaluate
$intd2 = int $d2; # will int create the value I want?
no.

$floatd2 = sprintf "%f",$d2; # will sprintf to a float give me what I
want?

no.

I was hoping to get the operators evaluated in the string expression?

You got the entire string evaluated in a numeric context. That's not at
all the same thing.
Is there anyway of doing this short of parsing out the operators and
setting up the math?
Yes.

Am I just doing something wrong here?
Yes.

Any help appreciated.

When Perl converts from a string to a number, it starts at the first
character of the string. If that character can start a valid number
(ex, a digit, a period (decimal), or a hypen (negative)), the number is
read until the first character in the string that makes an invalid
number. The substring from that point on is discarded. So the string
"15+0.5*30" evaluates in a numeric context to 15, because '+' is not a
valid character in the middle of a number.

The function you're looking for is 'eval'. Read about it at
perldoc -f eval

And beware the multitude of security concerns that haphazardly using
eval can create. Unless you *know* exactly what's in that string, eval
can cause a host of problems.

Hope this helps,
Paul Lalli
 
T

thundergnat

Bernard said:
Try this


$d2 = eval '15 + 0.5 * 30';



For more information:


perldoc -f eval
perldoc -f int


Something to keep in mind, especially if you don't have
firm control over the expressions to be eval()ed.

Try a line like:

print eval '015 + 015';

and see if you get the answer you expect.

30, right? Wrong.

Integer numbers with leading zeros are interpreted as being
octal.

Want to really blow your mind? Try this one:

print eval '01.5 + 01.5';

3? Not even close.

This drove me crazy until I figured out what was going on.
It IS documented behaviour, though not obviously.
 
A

A. Sinan Unur

This drove me crazy until I figured out what was going on.
It IS documented behaviour, though not obviously.

One way to deal with these issues might be to create a mini language
dealing with this kind of thing. The link below is my response to someone
who wanted to parse Excel formulas entered on the command line:

<URL: http://tinyurl.com/4mckr>

Please consider the code untested and unsafe. I ran it a few times before
posting it that time and have not looked at it since. It might be useful as
a starting point.

Sinan,
 

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
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top