string formulas to integers

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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.

Re: string formulas to integers

> 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.

<snip printed output>

> 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.

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

Re: string formulas to integers

nancy.iida wrote:

> 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

p

--
http://www.dunkelheit.at /
Das Tor zur Dunkelheit schwingt nur in eine Richtung...

Re: string formulas to integers

nancy.iida@dalsemi.com wrote:

> \$d2 = "15+0.5*30"; # this is the string I want to evaluate

[...]

> Here's the resultant output:
> d2 = 15+0.5*30
> intd2 = 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?

Try this

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

perldoc -f eval
perldoc -f int

--
Cheers,
Bernard

Re: string formulas to integers

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.

Re: string formulas to integers

Bernard El-Hagin wrote:
> nancy.iida@dalsemi.com wrote:
>
>>\$d2 = "15+0.5*30"; # this is the string I want to evaluate
>

>
> Try this
>
>
>   \$d2 = eval '15 + 0.5 * 30';
>
>
>
>
>
>   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.

Re: string formulas to integers

hQ@rcn.net:

> 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,