commit 37233b7232629a49591341f0815c060902ee849e parent abd8ed404671a1518c2ed757d1a57b870bf06f30 Author: Samir Parikh <noreply@samirparikh.com> Date: Tue, 28 Dec 2021 21:16:20 +0000 update to create stack Diffstat:
M | wordy/Wordy.pm | | | 30 | ++++++++++++++++-------------- |
1 file changed, 16 insertions(+), 14 deletions(-)
diff --git a/wordy/Wordy.pm b/wordy/Wordy.pm @@ -5,25 +5,27 @@ use v5.22; use Exporter qw<import>; our @EXPORT_OK = qw<answer>; -sub addition { $_[0] + $_[1] } -sub subtraction { $_[0] - $_[1] } -sub division { $_[0] / $_[1] } -sub multiplication { $_[0] * $_[1] } +my $pattern = qr/(-?\d+|plus|minus|divided|multiplied)/; sub answer { my ($question) = @_; die "unknown operation" if ($question !~ m/^What/); - my @m; say $question; - @m = ($question =~ - m/ - (-?\d+ # digits, optional minus sign - | # or - plus|minus|divided|multiplied) # operation - /gx); - say "here is what I found"; - say "->$_<-" foreach @m; - say "total elements: ", scalar @m; + my @m = ($question =~ m/$pattern/g); + #say "here is what I found"; + #say "->$_<-" foreach @m; + #say "total elements: ", scalar @m; + return $m[0] if (scalar @m == 1 && $m[0] =~ m/-?\d+/); + my @stack; + foreach (@m) { + push @stack => + m/-?\d+/ ? $_ : + $_ eq 'plus' ? '+' : + $_ eq 'minus' ? '-' : + $_ eq 'divided' ? '/' : + '*' ; + } + say "@stack"; return undef; }