From 4467e7406d93113bd8770780076ac90f29e4354d Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Tue, 17 Dec 2019 07:52:25 +1000 Subject: Fixed ch-2.pl --- challenge-039/ruben-westerberg/perl/ch-2.pl | 14 +++++--------- 1 file 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, $_; -- cgit