NateTut has asked for the wisdom of the Perl Monks concerning the following question:
Consider the following snippet:
The $Operator can be one of the following: +-*/
The code works fine but I was wondering if there is a better (or at least more compact or even different) way?
$Problem is in a format like: 1 + 1 =$Problem =~ /(\d*)\s(.)\s(\d*)/; my $Number1 = $1; my $Number2 = $3; my $Operator = $2; my $Answer; if($Operator eq '+') { $Answer = $Number1 + $Number2; } elsif($Operator eq '-') { $Answer = $Number1 - $Number2; } elsif($Operator eq '*') { $Answer = $Number1 * $Number2; } elsif($Operator eq '/') { $Answer = $Number1 / $Number2; }
The $Operator can be one of the following: +-*/
The code works fine but I was wondering if there is a better (or at least more compact or even different) way?
Back to
Seekers of Perl Wisdom