diff options
author | Gurusamy Sarathy <gsar@cpan.org> | 2000-05-05 02:01:50 +0000 |
---|---|---|
committer | Gurusamy Sarathy <gsar@cpan.org> | 2000-05-05 02:01:50 +0000 |
commit | 8ea1e5d40997f61551f59d826b2e5dd24e71e6b0 (patch) | |
tree | 65036d413c674a93dded263fb1a20d09f0a9bd0d /pod | |
parent | b319cd580f7af181ac94e0f1bec34de0d03328b0 (diff) | |
download | perl-8ea1e5d40997f61551f59d826b2e5dd24e71e6b0.tar.gz |
note about values()
p4raw-id: //depot/perl@6068
Diffstat (limited to 'pod')
-rw-r--r-- | pod/perlfunc.pod | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index 603d057dfb..8e66e1b5f8 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -2216,6 +2216,9 @@ or how about sorted by key: print $key, '=', $ENV{$key}, "\n"; } +The returned values are copies of the original keys in the hash, so +modifying them will not affect the original hash. Compare L</values>. + To sort a hash by value, you'll need to use a C<sort> function. Here's a descending numeric sort of a hash by its values: @@ -5417,12 +5420,11 @@ subject to change in future versions of perl, but it is guaranteed to be the same order as either the C<keys> or C<each> function would produce on the same (unmodified) hash. -Note that you cannot modify the values of a hash this way, because the -returned list is just a copy. You need to use a hash slice for that, -since it's lvaluable in a way that values() is not. +Note that the values are not copied, which means modifying them will +modify the contents of the hash: - for (values %hash) { s/foo/bar/g } # FAILS! - for (@hash{keys %hash}) { s/foo/bar/g } # ok + for (values %hash) { s/foo/bar/g } # modifies %hash values + for (@hash{keys %hash}) { s/foo/bar/g } # same As a side effect, calling values() resets the HASH's internal iterator. See also C<keys>, C<each>, and C<sort>. |