aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-039/ruben-westerberg/perl/ch-2.pl14
1 files changed, 5 insertions, 9 deletions
diff --git a/challenge-039/ruben-westerberg/perl/ch-2.pl b/challenge-039/ruben-westerberg/perl/ch-2.pl
index ba5d34dfba..c757cd0f98 100755
--- a/challenge-039/ruben-westerberg/perl/ch-2.pl
+++ b/challenge-039/ruben-westerberg/perl/ch-2.pl
@@ -1,6 +1,6 @@
#!/usr/bin/env perl
-my $expression=$ARGV[0]//"2 3 + 10 * 10 - 4 /";
+my $expression=$ARGV[0]//"15 7 1 1 + - / 3 * 2 1 1 + + -";
my @stack;
$expression=~s/ +/ /g;
print "Input expression: $expression\n";
@@ -8,23 +8,19 @@ for (split " ", $expression) {
die "Error in input expression" if !/[\-+]?\d+(\.\d+)*/ && !/[+\-*\/]/;
for ($_) {
if ($_ eq "+") {
- my @opds=splice @stack,0,2;
- push @stack, @opds[0]+@opds[1];
+ push @stack, pop(@stack)+ pop(@stack);
last;
}
if ($_ eq "-") {
- my @opds=splice @stack,0,2;
- push @stack, @opds[0]-@opds[1];
+ push @stack, pop(@stack)-pop(@stack);
last;
}
if ($_ eq "*") {
- my @opds=splice @stack,0,2;
- push @stack, @opds[0]*@opds[1];
+ push @stack, pop(@stack)*pop(@stack);
last;
}
if ($_ eq "/") {
- my @opds=splice @stack,0,2;
- push @stack, @opds[0]/@opds[1];
+ push @stack, pop(@stack)/pop(@stack);
last;
}
push @stack, $_;