Skip to content

Commit 1ec6200

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 cd68e69 commit 1ec6200

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

pod/perlfunc.pod

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,11 +2134,14 @@ X<array, iterator>
21342134
index/value from an array
21352135

21362136
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.
2137+
consisting of the key and value for the next element of a hash.
2138+
When called in scalar context, returns only the key (not the value).
2139+
2140+
When called on an array in list context, in Perl 5.12 and later, it
2141+
returns a 2-element list consisting of the index and value for the next
2142+
element of the array so that you can iterate over it; older Perls
2143+
consider this a syntax error. When called in scalar context, returns
2144+
only the index in the array.
21422145

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

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

21802194
The iterator used by C<each> is attached to the hash or array, and is
21812195
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
2196+
Thus all uses of C<each> on a particular hash or array advance the same
21832197
iterator location. All uses of C<each> are also subject to having the
21842198
iterator reset by any use of C<keys> or C<values> on the same hash or
21852199
array, or by the hash (but not array) being referenced in list context.
@@ -3934,10 +3948,12 @@ X<keys> X<key>
39343948
=for Pod::Functions retrieve list of indices from a hash or array
39353949

39363950
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
3951+
named hash, or in Perl 5.12 or later, the indices of an array. Perl
39383952
releases prior to 5.12 will produce a syntax error if you try to use an
39393953
array argument. In scalar context, returns the number of keys or indices.
39403954

3955+
Array entries are returned lowest index first.
3956+
39413957
Hash entries are returned in an apparently random order. The actual random
39423958
order is specific to a given hash; the exact same series of operations
39433959
on two hashes may result in a different order for each hash. Any insertion
@@ -3986,7 +4002,7 @@ sort of a hash by its values:
39864002
printf "%4d %s\n", $hash{$key}, $key;
39874003
}
39884004

3989-
Used as an lvalue C<keys> allows you to increase the
4005+
Used as an lvalue on a hash, C<keys> allows you to increase the
39904006
number of hash buckets
39914007
allocated for the given hash. This can gain you a measure of efficiency if
39924008
you know the hash is going to get big. (This is similar to pre-extending
@@ -4000,8 +4016,9 @@ buckets will be retained even if you do C<%hash = ()>, use C<undef
40004016
%hash> if you want to free the storage while C<%hash> is still in scope.
40014017
You can't shrink the number of buckets allocated for the hash using
40024018
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.
4019+
this by accident, as trying has no effect).
4020+
4021+
C<keys @array> in an lvalue context is a syntax error.
40054022

40064023
Starting with Perl 5.14, an experimental feature allowed
40074024
C<keys> to take a scalar expression. This experiment has
@@ -10497,10 +10514,12 @@ X<values>
1049710514

1049810515
=for Pod::Functions return a list of the values in a hash or array
1049910516

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.
10517+
Called in list context, returns a list consisting of all the value of the
10518+
named hash, or in Perl 5.12 or later, the values of an array. Perl
10519+
releases prior to 5.12 will produce a syntax error if you try to use an
10520+
array argument. In scalar context, returns the number of keys or indices.
10521+
10522+
Array entries are returned in the order of lowest index first.
1050410523

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

0 commit comments

Comments
 (0)