Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: My second script

by TedPride (Priest)
on May 21, 2006 at 23:56 UTC ( [id://550843]=note: print w/replies, xml ) Need Help??


in reply to My second script

I dislike using eval myself unless absolutely necessary. Why not just use linked subs:
use strict; use warnings; my (%ops, $first, $second, $op, $f); %ops = ( '+' => { 'label' => 'Sum', 'op' => sub { return $_[0] + $_[1]; } } +, '-' => { 'label' => 'Difference', 'op' => sub { return $_[0] - $_[ +1]; } }, '*' => { 'label' => 'Product', 'op' => sub { return $_[0] * $_[1]; + } }, '/' => { 'label' => 'Quotient', 'op' => sub { return $_[0] / $_[1] +; } } ); $ops{'M'} = { 'label' => 'Multiplication', 'table' => 1, 'op' => $ops{ +'*'}{'op'} }; sub getval { chomp($_ = <STDIN>); ($_) = m/($_[0])/ if $_[0]; return $_; } while (1) { print "Enter the first number: "; last if !($first = getval('\d+')); print "Enter the second number: "; last if !($second = getval('\d+')); print <<TEXT; Please enter the operation that you want to perform: + - * / M = Mulitiplication Table TEXT last if !$ops{$op = getval()}; print "\n"; if (!$ops{$op}{'table'}) { print "$ops{$op}{'label'} of $first $op $second is ", $ops{$op}{'op'}->($first, $second); } else { print ' ', join ' ', 1..$second; for $f (1..$first) { print "\n"; print join ' ', $f, map { $ops{$op}{'op'}->($f, $_) } 1..$ +second; } } print "\n\n"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found