Click here to get back home

Perl for calculating an expression.

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Perl for calculating an expression. syang8 07-23-2008
Get Chitika Premium
Posted by syang8 on July 23, 2008, 12:36 am
Please log in for more thread options
Is there any peal script available that I can quickly calculate a
string " (3+5)*5/40 " or " 4*7-7*(4+5) " ? There are only the digits
and +,-,*,/ and () available in the string. Thank you!

-Steve

Posted by Leon Timmermans on July 23, 2008, 6:40 am
Please log in for more thread options
On Tue, 22 Jul 2008 21:36:26 -0700, syang8 wrote:

> Is there any peal script available that I can quickly calculate a string
> " (3+5)*5/40 " or " 4*7-7*(4+5) " ? There are only the digits and
> +,-,*,/ and () available in the string. Thank you!
>
> -Steve

Look up Dijkstra's shunting yard algorithm. It's quite easy to implement.

Leon Timmermans

Posted by Glenn Jackman on July 23, 2008, 10:06 am
Please log in for more thread options
At 2008-07-23 12:36AM, "syang8" wrote:
> Is there any peal script available that I can quickly calculate a
> string " (3+5)*5/40 " or " 4*7-7*(4+5) " ? There are only the digits
> and +,-,*,/ and () available in the string. Thank you!

$str =~ m and $result = eval $1;

This does not check that your expression is "well formed" (e.g. $str="1+")

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous

Posted by Jürgen Exner on July 23, 2008, 10:50 am
Please log in for more thread options
>At 2008-07-23 12:36AM, "syang8" wrote:
>> Is there any peal script available that I can quickly calculate a
>> string " (3+5)*5/40 " or " 4*7-7*(4+5) " ? There are only the digits
>> and +,-,*,/ and () available in the string. Thank you!
>
> $str =~ m and $result = eval $1;
>
>This does not check that your expression is "well formed" (e.g. $str="1+")

Yes, it does:

        If there is a syntax error or runtime error, [...],
        an undefined value is returned by "eval", [...]

The code doesn't doesn't do any error handling, but that's a different
animal.

jue

Similar ThreadsPosted
How to convert string to an expression in PERL or calculating numerical string November 23, 2005, 5:03 am
Getting Perl to ignore Letters when calculating numbers. March 30, 2005, 9:54 pm
Calculating time of employee session from the log date/time stampusing perl May 24, 2005, 5:55 pm
calculating a list.. June 28, 2005, 4:51 am
ps2eps.pl Hangs on "Calculating Bounding Box" February 8, 2006, 7:45 am
Calculating values of a 2d array by comparison of 2 strings December 15, 2006, 9:48 am
Perl, Regular expression? December 23, 2005, 10:36 pm
perl regular expression help August 7, 2006, 2:47 am
perl regular expression April 13, 2007, 2:11 am
Regular Expression: Perl and vi August 13, 2007, 1:58 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap