Skip to content

Commit 7a4422e

Browse files
committed
perlfunc: Improve each, keys, values pod
The documentation for doing these operations on arrays was added at a later time to the documentation onf hashes. This cleans up some awkwardness and adds an example. Fixes #17719
1 parent 8400efe commit 7a4422e

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

pod/perlfunc.pod

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,15 +2130,17 @@ X<each> X<hash, iterator>
21302130
=item each ARRAY
21312131
X<array, iterator>
21322132

2133-
=for Pod::Functions retrieve the next key/value pair from a hash or
2134-
index/value from an array
2133+
=for Pod::Functions retrieve the next key/value pair from a hash or index/value from an array
21352134

21362135
When called on a hash in list context, returns a 2-element list
2137-
consisting of the key and value for the next element of a hash. In Perl
2138-
5.12 and later only, it will also return the index and value for the next
2139-
element of an array so that you can iterate over it; older Perls consider
2140-
this a syntax error. When called in scalar context, returns only the key
2141-
(not the value) in a hash, or the index in an array.
2136+
consisting of the key and value for the next element of a hash.
2137+
When called in scalar context, returns only the key (not the value).
2138+
2139+
When called on an array in list context, in Perl 5.12 and later, it
2140+
returns a 2-element list consisting of the index and value for the next
2141+
element of the array so that you can iterate over it; older Perls
2142+
consider this a syntax error. When called in scalar context, returns
2143+
only the index in the array.
21422144

21432145
Hash entries are returned in an apparently random order. The actual random
21442146
order is specific to a given hash; the exact same series of operations
@@ -2154,6 +2156,17 @@ details on why hash order is randomized. Aside from the guarantees
21542156
provided here, the exact details of Perl's hash algorithm and the hash
21552157
traversal order are subject to change in any release of Perl.
21562158

2159+
Array entries are returned lowest index first.
2160+
2161+
my @colors = (qw(red, green, blue));
2162+
while (my ($index, $value) = each @colors) {
2163+
print "[$index] = $value\n";
2164+
}
2165+
2166+
[0] = red
2167+
[1] = green
2168+
[2] = blue
2169+
21572170
After C<each> has returned all entries from the hash or
21582171
array, the next call to C<each> returns the empty list in
21592172
list context and L<C<undef>|/undef EXPR> in scalar context; the next
@@ -2179,7 +2192,7 @@ implementation.
21792192

21802193
The iterator used by C<each> is attached to the hash or array, and is
21812194
shared between all iteration operations applied to the same hash or array.
2182-
Thus all uses of C<each> on a single hash or array advance the same
2195+
Thus all uses of C<each> on a particular hash or array advance the same
21832196
iterator location. All uses of C<each> are also subject to having the
21842197
iterator reset by any use of C<keys> or C<values> on the same hash or
21852198
array, or by the hash (but not array) being referenced in list context.
@@ -3934,10 +3947,12 @@ X<keys> X<key>
39343947
=for Pod::Functions retrieve list of indices from a hash or array
39353948

39363949
Called in list context, returns a list consisting of all the keys of the
3937-
named hash, or in Perl 5.12 or later only, the indices of an array. Perl
3950+
named hash, or in Perl 5.12 or later, the indices of an array. Perl
39383951
releases prior to 5.12 will produce a syntax error if you try to use an
39393952
array argument. In scalar context, returns the number of keys or indices.
39403953

3954+
Array entries are returned lowest index first.
3955+
39413956
Hash entries are returned in an apparently random order. The actual random
39423957
order is specific to a given hash; the exact same series of operations
39433958
on two hashes may result in a different order for each hash. Any insertion
@@ -3986,7 +4001,7 @@ sort of a hash by its values:
39864001
printf "%4d %s\n", $hash{$key}, $key;
39874002
}
39884003

3989-
Used as an lvalue C<keys> allows you to increase the
4004+
Used as an lvalue on a hash, C<keys> allows you to increase the
39904005
number of hash buckets
39914006
allocated for the given hash. This can gain you a measure of efficiency if
39924007
you know the hash is going to get big. (This is similar to pre-extending
@@ -4000,8 +4015,9 @@ buckets will be retained even if you do C<%hash = ()>, use C<undef
40004015
%hash> if you want to free the storage while C<%hash> is still in scope.
40014016
You can't shrink the number of buckets allocated for the hash using
40024017
C<keys> in this way (but you needn't worry about doing
4003-
this by accident, as trying has no effect). C<keys @array> in an lvalue
4004-
context is a syntax error.
4018+
this by accident, as trying has no effect).
4019+
4020+
C<keys @array> in an lvalue context is a syntax error.
40054021

40064022
Starting with Perl 5.14, an experimental feature allowed
40074023
C<keys> to take a scalar expression. This experiment has
@@ -10497,10 +10513,12 @@ X<values>
1049710513

1049810514
=for Pod::Functions return a list of the values in a hash or array
1049910515

10500-
In list context, returns a list consisting of all the values of the named
10501-
hash. In Perl 5.12 or later only, will also return a list of the values of
10502-
an array; prior to that release, attempting to use an array argument will
10503-
produce a syntax error. In scalar context, returns the number of values.
10516+
Called in list context, returns a list consisting of all the value of the
10517+
named hash, or in Perl 5.12 or later, the values of an array. Perl
10518+
releases prior to 5.12 will produce a syntax error if you try to use an
10519+
array argument. In scalar context, returns the number of keys or indices.
10520+
10521+
Array entries are returned in the order of lowest index first.
1050410522

1050510523
Hash entries are returned in an apparently random order. The actual random
1050610524
order is specific to a given hash; the exact same series of operations

0 commit comments

Comments
 (0)