aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Westerberg <drclaw@mac.com>2019-12-17 07:52:25 +1000
committerRuben Westerberg <drclaw@mac.com>2019-12-17 07:52:25 +1000
commit4467e7406d93113bd8770780076ac90f29e4354d (patch)
tree0fa78cdb5bf8c0f0edc11e70f26bce7546f469d0
parent88b62335e7b5952402e42ccb90c67eee3048f49d (diff)
downloadperlweeklychallenge-club-4467e7406d93113bd8770780076ac90f29e4354d.tar.gz
perlweeklychallenge-club-4467e7406d93113bd8770780076ac90f29e4354d.tar.bz2
perlweeklychallenge-club-4467e7406d93113bd8770780076ac90f29e4354d.zip
Fixed ch-2.pl
-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, $_;