-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLength.pm
137 lines (91 loc) · 3.07 KB
/
Length.pm
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
package List::Value::Length;
use 5.006;
use strict;
use warnings;
use Exporter qw(import);
our $VERSION = '1.0';
our @EXPORT_OK = qw(
longest_length shortest_length
longest_value shortest_value
longest_values shortest_values
);
sub longest_length {
my $longest_length = 0;
for my $item (@_) {
$longest_length = length($item) if ( !$longest_length || length($item) > $longest_length );
}
return $longest_length;
}
sub shortest_length {
my $shortest_length = 0;
for my $item (@_) {
$shortest_length = length($item) if ( !$shortest_length || length($item) < $shortest_length );
}
return $shortest_length;
}
sub longest_value {
my $longest_length = longest_length(@_);
my @long_list;
for my $item (@_) {
push @long_list, $item if length($item) == $longest_length;
}
return @long_list;
}
sub shortest_value {
my $shortest_length = shortest_length(@_);
my @short_list;
for my $item (@_) {
push @short_list, $item if length($item) == $shortest_length;
}
return @short_list;
}
sub longest_values {
longest_value(@_);
}
sub shortest_values {
shortest_value(@_);
}
=pod
=encoding utf8
=head1 NAME
B<List::Value::Length> returns the length of the longest or shortest value or the longest or shortest value(s) on the list.
=head1 VERSION
This document describes List::Value::Length version 1.0.
=head1 SYNOPSIS
use List::Value::Length qw(longest_value shortest_value longest_length shortest_length);
my @colors = ('red', 'orange', 'yellow', 'spring', 'green', 'teal', 'cyan', 'azure',
'blue', 'violet', 'magenta', 'pink', 'white', 'black', 'gray');
my $longest_length = longest_length(@colors);
my $shortest_length = shortest_length(@colors);
my @longest_values = longest_value(@colors);
my @shortest_values = shortest_value(@colors);
=head1 DESCRIPTION
=head2 longest_length
my $longest_length = longest_length(@colors);
C<longest_length> returns the longest integer length of the values of an array.
It will return C<7> for the above example.
=head2 shortest_length
my $shortest_length = shortest_length(@colors);
C<shortest_length> returns the shortest integer length of the values of an array.
It will return C<3> for the above example.
=head2 longest_value
my @longest_values = longest_value(@colors);
C<longest_value> returns an array with all the values that are the longest length.
It will return the following for the preceding example.
( 'magenta' )
C<longest_values>, the plural form, also works.
=head2 shortest_value
my @shortest_values = shortest_value(@colors);
C<shortest_value> returns an array with all the values that are the shortest length.
It will return the following for the preceding example.
( 'red' )
C<shortest_values>, the plural form, also works.
=head1 DEPENDENCY
List::Value::Length depends on L<Exporter>.
=head1 AUTHOR
Lady Aleena
=head1 LICENSE AND COPYRIGHT
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L<perlartistic>.
Copyright © 2020, Lady Aleena C<(aleena@cpan.org)>. All rights reserved.
=cut
1;