-
Notifications
You must be signed in to change notification settings - Fork 314
/
ch-2.pl
143 lines (114 loc) · 3.71 KB
/
ch-2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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 ($option, @rest) = @ARGV;
#choose action from the given option or invoke the default one
my $action = $option && $actions->{$option} || $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')