From 882d1fe2d73fa574b69becc831ebb9419ebb22da Mon Sep 17 00:00:00 2001 From: Ysmael Ebreo Date: Wed, 26 Feb 2020 16:53:44 +0800 Subject: Added solution ch-34 --- challenge-034/yet-ebreo/perl5/ch-1.pl | 28 ++++++++++++++++++++++++ challenge-034/yet-ebreo/perl5/ch-2.pl | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 challenge-034/yet-ebreo/perl5/ch-1.pl create mode 100644 challenge-034/yet-ebreo/perl5/ch-2.pl diff --git a/challenge-034/yet-ebreo/perl5/ch-1.pl b/challenge-034/yet-ebreo/perl5/ch-1.pl new file mode 100644 index 0000000000..b9aabf6867 --- /dev/null +++ b/challenge-034/yet-ebreo/perl5/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +my @array = qw(a quick brown fox jumps over the lazy dog); + +#Using index +say $array[4]; + +#Using multiple indices +my @sliced_array = @array[2,3]; +say @sliced_array; + +#Using range +@sliced_array = @array[2..6]; + +say @sliced_array; + +my $n = 0; +my %hash = map { $_ => $n++ } @array; + +#Using one key +say $hash{'jumps'}; + +#Using array as keys +say @hash{qw(jumps dog a)}; \ No newline at end of file diff --git a/challenge-034/yet-ebreo/perl5/ch-2.pl b/challenge-034/yet-ebreo/perl5/ch-2.pl new file mode 100644 index 0000000000..a810ecf774 --- /dev/null +++ b/challenge-034/yet-ebreo/perl5/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +my %table = ( + add => \&add, + subtract => \&subtract, + multiply => \&multiply, + divide => \÷ +); + +sub divide { + my ($m,$n) = @_; + say "Error: Divide by 0" if !$n; + + return $m/$n; +} + +sub add { + my ($m,$n) = @_; + return $m+$n; +} + +sub subtract { + my ($m,$n) = @_; + return $m-$n; +} + +sub multiply { + my ($m,$n) = @_; + return $m*$n; +} + +my $commands = "add multiply subtract divide "; +my @operands = (43,6); +for my $cmd ($commands=~/(\S+) /g) { + say "[$cmd]"; + say $table{$cmd}->(@operands); +} \ No newline at end of file -- cgit