Perl - What. The. Hell. - Arithmetic with Scalar?

TurnerTurner Registered User regular
edited October 2018 in Help / Advice Forum
deleted.

Turner on

Posts

  • DrFrylockDrFrylock Registered User regular
    edited September 2008
    Not knowing Perl, I think the thing you're looking for is probably eval, or some variant thereof. What you want to do is treat the read-in string as a little Perl program to be executed.

    DrFrylock on
  • TurnerTurner Registered User regular
    edited October 2018
    deleted.

    Turner on
  • GdiguyGdiguy San Diego, CARegistered User regular
    edited September 2008
    http://stein.cshl.org/cgi-bin/perlref?func=eval
    eval $x; # CASE 1
    eval "$x"; # CASE 2

    eval '$x'; # CASE 3
    eval { $x }; # CASE 4

    eval "\$$x++"; # CASE 5
    $$x++; # CASE 6

    Cases 1 and 2 above behave identically: they run the code
    contained in the variable $x. (Although case 2 has misleading
    double quotes making the reader wonder what else might be
    happening (nothing is).) Cases 3 and 4 likewise behave in the
    same way: they run the code '$x', which does nothing but return
    the value of $x. (Case 4 is preferred for purely visual reasons,
    but it also has the advantage of compiling at compile-time
    instead of at run-time.) Case 5 is a place where normally you
    *would* like to use double quotes, except that in this
    particular situation, you can just use symbolic references
    instead, as in case 6.

    my $blah = ".5 + .5 + .5";
    print "$blah\n";
    print $blah;
    print "\n";

    my $blah2 = eval $blah;
    print "$blah2\n";

    blah2 outputs 1.5


    I've never used eval before, but it looks like it does exactly what you want

    Gdiguy on
  • SevorakSevorak Registered User regular
    edited September 2008
    It's not really putting quotation marks around the input. Perl is a dynamically typed language, which means the type of your variable is determined by what you assign to it. Perl, and pretty much every other programming language, reads data from a file as a string, so the variable you assign the data to will be a string. In order to evaluate the expression you will have to parse the string into a form that is possible for an algorithm to evaluate. This involves tokenizing the string, converting it to reverse Polish notation, and evaluating then equation using a stack. This is not a trivial task, and it's beyond my ability to explain since I haven't done it in some time, but looking up equation parser in google should help you out.

    Edit: Or maybe there's a library function that does it for you. Guess I don't know Perl very well.

    Sevorak on
    steam_sig.png 3DS: 0748-2282-4229
  • RhinoRhino TheRhinLOL Registered User regular
    edited September 2008
    # convert to numbers 
    my ($var1, $var2, $var3) = split (/\s{0,}\+\s{0,}/, $testExpression);
    $testExpression = $var1 + $var2 + $var3;
    

    Rhino on
    93mb4.jpg
  • TurnerTurner Registered User regular
    edited October 2018
    deleted.

    Turner on
  • GdiguyGdiguy San Diego, CARegistered User regular
    edited September 2008
    Rhino wrote: »
    # convert to numbers 
    my ($var1, $var2, $var3) = split (/\s{0,}\+\s{0,}/, $testExpression);
    $testExpression = $var1 + $var2 + $var3;
    

    So you could do that, but eval would be way more flexible if you want to be able to do something other than addition (I'm not actually sure what the easiest way to do that would be)

    Gdiguy on
  • TurnerTurner Registered User regular
    edited October 2018
    deleted.

    Turner on
  • Smug DucklingSmug Duckling Registered User regular
    edited September 2008
    Yeah, I do a lot of Perl and eval is definitely the right way to do this.

    Here's an example of a quick and dirty command-line calculator that I wrote that basically just uses Perl's parser to calculate by using eval:
    my $args = join(" ", @ARGV);
    my $calc = "print($args);";
    my $return = eval $calc;
    

    You would type something like:

    perl calc.pl 1 + 2 - 8 / 2

    Smug Duckling on
    smugduckling,pc,days.png
Sign In or Register to comment.