in reply to '+' to +
#!perl use warnings; use strict; my %arith; $arith{"+"} = sub { $_[0] + $_[1] }; $arith{"-"} = sub { $_[0] - $_[1] }; $arith{"*"} = sub { $_[0] * $_[1] }; $arith{"/"} = sub { $_[0] / $_[1] }; my $Problem = "6 * 8"; $Problem =~ /^(\d*)\s(.)\s(\d*)$/; my $Number1 = $1; my $Number2 = $3; my $Operator = $2; my $Op = $arith{$2} or die qq[wrong operator: "$Operator"]; my $Answer; $Answer = &$Op($Number1, $Number2); print $Answer, "\n"; __END__
And take care with division by zero errors.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: '+' to +
by davido (Cardinal) on May 29, 2005 at 16:19 UTC | |
|
Re^2: '+' to +
by ihb (Deacon) on May 29, 2005 at 16:37 UTC | |
|
Re^2: '+' to +
by NateTut (Deacon) on May 29, 2005 at 14:13 UTC |