Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

string to expression

by xiaoyafeng (Deacon)
on Jan 10, 2007 at 02:34 UTC ( [id://593816]=perlquestion: print w/replies, xml ) Need Help??

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

Hi respected monks!

I've written a calculated script just for fun.Below is my code:
print "Welcome to calculator script!!\n"; print "Please choose calculator range:\n"; print "-100 -- +100 => 1 (default)\n"; print "-10 -- +10 => 2 \n"; print "your choice:"; my $choice = <>; chomp $choice; my @operators = qw(+ - * /); my ($value1,$value2,$true_answer,$answer,$time_cal,$operator); die "error !!! please check $!\n" unless $choice == 1 or $choice == 2; if ($choice == 1) { $value1 = int rand(100); $value2 = int rand(100); } else { $value1 = int rand(10); $value2 = int rand(10); } $operator = $operators[int rand(4)]; $true_answer = $value1 + $value2 if $operator eq $operators[0]; $true_answer = $value1 - $value2 if $operator eq $operators[1]; $true_answer = $value1 * $value2 if $operator eq $operators[2]; $true_answer = int($value1 / $value2) if $operator eq $operators[3]; print "$value1$operator$value2="; $time_cal = time; eval{ $answer = <>; $time_cal = time - $time_cal; die "you are wrong!!!!\n" unless $answer == $true_answer; }; die "please input decimal number!!!!\n" if $@ ; print "you spend $time_cal seconds!!!!\n";
my question is: Could I change a string to a mathematics expression directly?So I needn't change (* / + -) to arithmetic operator one by one.

Thanks in advance! UPDATE: Thank liverpole,and correct some ridiculous mistakes.


I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: string to expression
by liverpole (Monsignor) on Jan 10, 2007 at 02:49 UTC
    Hi xiaoyafeng,

    You could use a dispatch table.

    For example:

    my $pdispatch = { '+' => sub { $_[0] + $_[1] }, '-' => sub { $_[0] - $_[1] }, '*' => sub { $_[0] * $_[1] }, '/' => sub { $_[0] / $_[1] }, }; # and later ... my $operator = qw(+ - * /)[int rand 4]; my $true_answer = $pdispatch->{$operator}->($value1, $value2);

    The dispatch table $pdispatch is a hash reference, where each key evaluates to an anonymous subroutine to perform that operation on its two arguments.

    The operator $operator is randomly selected from a list of 4 operators, and $true_answer is assigned the results of the subroutine for the random operator, performed on its 2 arguments.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: string to expression
by GrandFather (Saint) on Jan 10, 2007 at 03:05 UTC

    Maybe you just want eval? Consider:

    use strict; use warnings; my @operators = qw(+ - * /); my $expression = (int rand(100)) . $operators[int rand(4)] . (int rand +(100)); my $answer = eval $expression; print "$expression = $answer";

    Prints (for example):

    78+63 = 141

    DWIM is Perl's answer to Gödel
      Great!! eval looks like a best way! Thank you! I'm going to read perlfunc.

      btw.any other tutorial for eval?
      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction
        Tutorial? eval EXPR runs Perl code. Any Perl code. 78+63 happens to be valid Perl. (For that reason, you'd be better with liverpole's dispatch table.)
Re: string to expression
by mkirank (Chaplain) on Jan 10, 2007 at 03:35 UTC
    check overload
    #!perl use strict; use warnings; my $num = new SomeThing 57; $b=5+$num; print "b is $b \n"; my $minus = $num - 10; print "minus is $minus \n"; package SomeThing; sub new { my $p = shift; bless [@_], $p } use overload '+' => \&myadd, '-' => \&mysub; sub myadd { my $self = shift; my $var = shift; return ($var + $self->[0]); } sub mysub { my $self = shift; my $var = shift; return ( $self->[0] - $var); } 1;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://593816]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-03-28 19:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found