Perl for calculating an expression.

Discussion in 'Perl Misc' started by syang8, Jul 23, 2008.

  1. syang8

    syang8 Guest

    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
    syang8, Jul 23, 2008
    #1
    1. Advertising

  2. 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
    Jürgen Exner, Jul 23, 2008
    #2
    1. Advertising

  3. [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
    Ilya Zakharevich, Jul 23, 2008
    #3
  4. 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
    Jürgen Exner, Jul 23, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Asad
    Replies:
    2
    Views:
    434
    Matt Berther
    Apr 27, 2004
  2. Sparky Arbuckle

    Calculating a Subtotal for Shopping Cart

    Sparky Arbuckle, Mar 6, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    1,620
    Sparky Arbuckle
    Mar 6, 2005
  3. Nathan Sokalski

    Calculating the width of an element

    Nathan Sokalski, May 30, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    453
    Herfried K. Wagner [MVP]
    May 30, 2005
  4. Jayesh Kamdar
    Replies:
    2
    Views:
    144
    Gunnar Hjalmarsson
    May 25, 2005
  5. rajesh
    Replies:
    0
    Views:
    168
    rajesh
    Nov 23, 2005
Loading...

Share This Page