diff options
| -rw-r--r-- | challenge-039/ndelucca/perl5/ch-2.pl | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-039/ndelucca/perl5/ch-2.pl b/challenge-039/ndelucca/perl5/ch-2.pl new file mode 100644 index 0000000000..3cf6a4b9a4 --- /dev/null +++ b/challenge-039/ndelucca/perl5/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-039/ +# Task #2 +# Write a script to demonstrate Reverse Polish notation(RPN). +# Checkout the wiki page for more information about RPN. +# https://en.wikipedia.org/wiki/Reverse_Polish_notation + +use strict; +use warnings; + +die "Input required" unless @ARGV; + +my @stack; +my @rpn = split / /, shift; + +while (@rpn) { + + my $in = shift @rpn; + + if ($in =~ /^\d+$/) { + push @stack, $in; + }else{ + my $num1 = pop @stack; + my $num2 = pop @stack; + push @stack, eval "$num2 $in $num1"; + } +} + +print "Result: ",@stack, "\n"; |
