...
/Solution Review: Batch Calculator
Solution Review: Batch Calculator
See the solution to the batch calculator exercise.
We'll cover the following...
Solution
Let’s look at the solution before jumping into the explanation:
Press + to interact
my %operations = (plus => sub { $_[0] + $_[1] },minus => sub { $_[0] - $_[1] },times => sub { $_[0] * $_[1] },dividedby => sub { $_[0] / $_[1] },raisedto => sub { $_[0] ** $_[1] },);sub calculate {my (@array) = @_;my @results;foreach my $subarray (@array) {my ($operand1, $op, $operand2) = @$subarray;next unless exists $operations{ $op } and @$subarray == 3;push @results, $operations{ $op }->( $operand1, $operand2 );}print "@results";}
Explanation
Let's go through the solution step by step: