diff options
| author | andrezgz <andrezgz@gmail.com> | 2019-11-16 01:08:47 -0300 |
|---|---|---|
| committer | andrezgz <andrezgz@gmail.com> | 2019-11-16 01:08:47 -0300 |
| commit | 365f9121394bf2274425aa1e5e9a10f946734e4b (patch) | |
| tree | 30cc10e4c89670786787cb0b5a6bf3a70c26e20c | |
| parent | bc851e89dd27081852cdf4922786f2961433a2fc (diff) | |
| download | perlweeklychallenge-club-365f9121394bf2274425aa1e5e9a10f946734e4b.tar.gz perlweeklychallenge-club-365f9121394bf2274425aa1e5e9a10f946734e4b.tar.bz2 perlweeklychallenge-club-365f9121394bf2274425aa1e5e9a10f946734e4b.zip | |
challenge-034 andrezgz solution
| -rw-r--r-- | challenge-034/andrezgz/perl5/ch-1.pl | 143 | ||||
| -rw-r--r-- | challenge-034/andrezgz/perl5/ch-2.pl | 143 |
2 files changed, 286 insertions, 0 deletions
diff --git a/challenge-034/andrezgz/perl5/ch-1.pl b/challenge-034/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..043139a9b9 --- /dev/null +++ b/challenge-034/andrezgz/perl5/ch-1.pl @@ -0,0 +1,143 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-034/ +# Task #1 +# Write a program that demonstrates using hash slices and/or array slices. + +use strict; +use warnings; + +my $heroes_identity = { + 'Superman' => 'Clark Kent', + 'Batman' => 'Bruce Wayne', + 'Flash' => 'Barry Allen', + 'Aquaman' => 'Arthur Curry', + 'Deadpool' => 'Wade Wilson', + 'Hawkeye' => 'Clint Barton', + 'Hulk' => 'Bruce Banner', +}; + +my $actions = { + '--help' => \&help, + '--array-slice' => \&array_slice, + '--hash-slice' => \&hash_slice, + '-h' => \&help, + '-as' => \&array_slice, + '-hs' => \&hash_slice, +}; + +my ($param, @rest) = @ARGV; +#choose action from +my $action = $param && $actions->{$param} || $actions->{'--help'}; +$action->($heroes_identity,@rest); +exit 0; + +sub array_slice { + my ($heroes_identity,$offset,$length) = @_; + + my @heroes = sort keys %{$heroes_identity}; + print 'Array of heroes:' . aref_to_string(\@heroes) . "\n"; + + my $slice_mssg = ''; + my @slice; + if (defined $offset && defined $length) { + @slice = splice @heroes, $offset, $length; + $slice_mssg = "Array slice of $length elements from position $offset"; + } + elsif (defined $offset) { + @slice = splice @heroes, $offset; + print "Array slice of elements from position $offset"; + } + else { + @slice = splice @heroes; + print "Array slice of elements"; + } + print $slice_mssg . ':' . aref_to_string(\@slice) . "\n"; + + print 'Array of remaining elements:' . aref_to_string(\@heroes) . "\n"; + return; +} + +sub hash_slice { + my ($heroes_identity,@sel_heroes) = @_; + @sel_heroes = grep { exists $heroes_identity->{$_} } + map { ucfirst } + @sel_heroes; + + if ( @sel_heroes ) { + my @sel_heroes_identity = @{$heroes_identity}{ @sel_heroes}; + print 'Hash of heroes:' . href_to_string($heroes_identity) . "\n"; + print 'Array of existing given heroes:' . aref_to_string(\@sel_heroes) . "\n"; + print 'Hash slice of existing given heroes:' . aref_to_string(\@sel_heroes_identity) . "\n"; + } + else { + print "None of the given heroes exist\n"; + } + return; +} + + +sub help { + print <<"EOT"; +Usage: $0 <option> <arguments> + +Options: + -h, --help display this help and exit. + + -hs, --hash-slice print a hash slice example for the given heroes. + arguments: [<heroe> ...] + example: $0 --hash-slice Superman Batman + + -as, --array-slice print an array slice example of <length> heroes, + starting from position <offset>. + arguments: [<offset> | <offset> <length> ] + example: $0 --array-slice 2 3 + +EOT + return; +} + + +sub aref_to_string { + my $aref = shift; + my $ret = join ',', map { "'$_'" } @{$aref}; + return "\n($ret)\n"; +} + +sub href_to_string { + my $href = shift; + my $ret = join "\n", map { sprintf "%12s => '%s'","'$_'",$href->{$_} } sort keys %{$href}; + return "\n(\n$ret\n)\n"; +} + +__END__ + +./ch-1.pl --hash-slice Superman Batman +Hash of heroes: +( + 'Aquaman' => 'Arthur Curry' + 'Batman' => 'Bruce Wayne' + 'Deadpool' => 'Wade Wilson' + 'Flash' => 'Barry Allen' + 'Hawkeye' => 'Clint Barton' + 'Hulk' => 'Bruce Banner' + 'Superman' => 'Clark Kent' +) + +Array of existing given heroes: +('Superman','Batman') + +Hash slice of existing given heroes: +('Clark Kent','Bruce Wayne') + + + +./ch-1.pl --array-slice 2 3 +Array of heroes: +('Aquaman','Batman','Deadpool','Flash','Hawkeye','Hulk','Superman') + +Array slice of 3 elements from position 2: +('Deadpool','Flash','Hawkeye') + +Array of remaining elements: +('Aquaman','Batman','Hulk','Superman') diff --git a/challenge-034/andrezgz/perl5/ch-2.pl b/challenge-034/andrezgz/perl5/ch-2.pl new file mode 100644 index 0000000000..1d7268f250 --- /dev/null +++ b/challenge-034/andrezgz/perl5/ch-2.pl @@ -0,0 +1,143 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-034/ +# Task #2 +# Write a program that demonstrates a dispatch table. + +use strict; +use warnings; + +my $heroes_identity = { + 'Superman' => 'Clark Kent', + 'Batman' => 'Bruce Wayne', + 'Flash' => 'Barry Allen', + 'Aquaman' => 'Arthur Curry', + 'Deadpool' => 'Wade Wilson', + 'Hawkeye' => 'Clint Barton', + 'Hulk' => 'Bruce Banner', +}; + +my $actions = { + '--help' => \&help, + '--array-slice' => \&array_slice, + '--hash-slice' => \&hash_slice, + '-h' => \&help, + '-as' => \&array_slice, + '-hs' => \&hash_slice, +}; + +my ($param, @rest) = @ARGV; +#choose action from +my $action = $param && $actions->{$param} || $actions->{'--help'}; +$action->($heroes_identity,@rest); +exit 0; + +sub array_slice { + my ($heroes_identity,$offset,$length) = @_; + + my @heroes = sort keys %{$heroes_identity}; + print 'Array of heroes:' . aref_to_string(\@heroes) . "\n"; + + my $slice_mssg = ''; + my @slice; + if (defined $offset && defined $length) { + @slice = splice @heroes, $offset, $length; + $slice_mssg = "Array slice of $length elements from position $offset"; + } + elsif (defined $offset) { + @slice = splice @heroes, $offset; + print "Array slice of elements from position $offset"; + } + else { + @slice = splice @heroes; + print "Array slice of elements"; + } + print $slice_mssg . ':' . aref_to_string(\@slice) . "\n"; + + print 'Array of remaining elements:' . aref_to_string(\@heroes) . "\n"; + return; +} + +sub hash_slice { + my ($heroes_identity,@sel_heroes) = @_; + @sel_heroes = grep { exists $heroes_identity->{$_} } + map { ucfirst } + @sel_heroes; + + if ( @sel_heroes ) { + my @sel_heroes_identity = @{$heroes_identity}{ @sel_heroes}; + print 'Hash of heroes:' . href_to_string($heroes_identity) . "\n"; + print 'Array of existing given heroes:' . aref_to_string(\@sel_heroes) . "\n"; + print 'Hash slice of existing given heroes:' . aref_to_string(\@sel_heroes_identity) . "\n"; + } + else { + print "None of the given heroes exist\n"; + } + return; +} + + +sub help { + print <<"EOT"; +Usage: $0 <option> <arguments> + +Options: + -h, --help display this help and exit. + + -hs, --hash-slice print a hash slice example for the given heroes. + arguments: [<heroe> ...] + example: $0 --hash-slice Superman Batman + + -as, --array-slice print an array slice example of <length> heroes, + starting from position <offset>. + arguments: [<offset> | <offset> <length> ] + example: $0 --array-slice 2 3 + +EOT + return; +} + + +sub aref_to_string { + my $aref = shift; + my $ret = join ',', map { "'$_'" } @{$aref}; + return "\n($ret)\n"; +} + +sub href_to_string { + my $href = shift; + my $ret = join "\n", map { sprintf "%12s => '%s'","'$_'",$href->{$_} } sort keys %{$href}; + return "\n(\n$ret\n)\n"; +} + +__END__ + +./ch-2.pl --hash-slice Superman Batman +Hash of heroes: +( + 'Aquaman' => 'Arthur Curry' + 'Batman' => 'Bruce Wayne' + 'Deadpool' => 'Wade Wilson' + 'Flash' => 'Barry Allen' + 'Hawkeye' => 'Clint Barton' + 'Hulk' => 'Bruce Banner' + 'Superman' => 'Clark Kent' +) + +Array of existing given heroes: +('Superman','Batman') + +Hash slice of existing given heroes: +('Clark Kent','Bruce Wayne') + + + +./ch-2.pl --array-slice 2 3 +Array of heroes: +('Aquaman','Batman','Deadpool','Flash','Hawkeye','Hulk','Superman') + +Array slice of 3 elements from position 2: +('Deadpool','Flash','Hawkeye') + +Array of remaining elements: +('Aquaman','Batman','Hulk','Superman') |
