Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Small Calculator!
by BrowserUk (Patriarch) on Oct 04, 2002 at 20:34 UTC

    A golf calculator? 29.

    C:\test>perl -le"print eval while<>;" 2+2 4 3*5 15 $a=3**2+4 13 $a/2 6.5 (1+(2*3)+4)/($a%3) 11 ^Z C:\test>

    Or with a Log10 function: 53. (Note: capital L)

    C:\test>perl -le"sub Log{log(pop)/log(10)}print eval while<>;" Log(100) 2 10**2 100 Log(30)+log(40) 5.1660007088336 10**(Log(30)+Log(40)) 1200 exp(log(30)+log(40)) 1200

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
      The -l is just used to add a newline to each print?

      Why not use -p flag too, and have it do the reading and printing?

      perl -lpe"$_=eval"
      (works in a file, -e has issues under Windows)

        Adding some error checking...

        perl -ple'$_=eval||$@'

        A calculator really should be able to tell you not to divide by 0, don't you agree? :-)

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: Small Calculator!
by husoft (Monk) on Oct 04, 2002 at 19:19 UTC
    #!/usr/bin/perl -wnl $_=eval();print;
    This will do the work!
      #!/usr/local/bin/perl -w $|++; print "calc: "; print eval, "\ncalc: " while chomp( $_ = <STDIN> );
      Pretty sure this code is simmilar to some other code I found round here somewhere, heh. It should do the job for you..

      -- Can't never could do anything, so give me and inch, I'll make it a mile.

Re: Small Calculator!
by shotgunefx (Parson) on Oct 04, 2002 at 19:20 UTC
    You could look at Evaluate Expressions.. It doesn't have log() but trivial to add. There is also a reply with a CPAN module for it as well.

    -Lee

    "To be civilized is to deny one's nature."
Re: Small Calculator!
by swiftone (Curate) on Oct 04, 2002 at 19:20 UTC
    You'll find few monks willing to assist on what could very well be a homework problem unless you show some indication you've tried it yourself. Show us some code, and we'll help you along.

    Here's hint though. Look at the eval() function.