Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl for calculating an expression.

Reply
Thread Tools

Perl for calculating an expression.

 
 
syang8
Guest
Posts: n/a
 
      07-23-2008
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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      07-23-2008
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!


Is quick and dirty good enough? Then just use eval().

Otherwise writing a simple parser and interpreter for such a tiny
language is a favourite second semester excercise.

jue
 
Reply With Quote
 
 
 
 
Ilya Zakharevich
Guest
Posts: n/a
 
      07-23-2008
[A complimentary Cc of this posting was NOT [per weedlist] sent to
Jürgen Exner
<>], who wrote in article <>:
> 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!

>
> Is quick and dirty good enough? Then just use eval().
>
> Otherwise writing a simple parser and interpreter for such a tiny
> language is a favourite second semester excercise.


Here are my aliases (4dos and tcsh):

>alias pprint

pprint -e "print {STDOUT} %&"

>alias _pprint

perl -wle "sub atan($) {atan2 shift, 1} sub tan($) {sin($_[0])/cos($_[0])} sub pi () {atan2 0, -1} sub e() {exp 1} sub h() {'6.62607e-34'} sub eV() {'1.6021765e-19'} sub G() {6.674e-11} sub N() {6.022142e23} sub c() {299792458} sub R() {8.314472} sub k() {R/N} $, = q(, ); "

->alias pprint
perl -wle '$, = q(, ); print(!:1*)'

Hope this helps,
Ilya
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      07-23-2008
Glenn Jackman <> wrote:
>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{^([\s\d()/*+-]+)$} 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
How to convert string to an expression in PERL or calculating numerical string rajesh Perl Misc 0 11-23-2005 01:03 PM
Calculating time of employee session from the log date/time stampusing perl Jayesh Kamdar Perl Misc 2 05-25-2005 12:08 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments