The thread "Randomly reassign hash keys" got me thinking about this one. The values doc says:
Note that the values are not copied, which means modifying them will modify the contents of the hash.
This means that $_++ for values %hash; will modify the original hash values, and the following (based on this AM post) will assign to the values of the hash:
sub { @_[0..$#_] = qw/r s t/ }->(values %hash);
The above work because both the foreach loop variable and the elements of @_ are aliases to the original scalars. The slice in the second example is necessary, since @_ = ... would clobber the array and remove the aliasing, instead of assigning to its elements.
However, values itself is not lvalue, meaning that values(%hash) = qw/a b c/; won't work. Neither will (values(%hash))[0] = 'a';.
Here's my "invention", which I present as a curiosity rather than a suggested solution. It makes use of Lvalue subroutines and Prototypes. The parens on the left are necessary for the sub to be called in list context.
sub lvalues (\%) : lvalue { values %{$_[0]} } (lvalues %hash) = qw/i j k/; # works!
Since the order of the values returned by values is random, this probably isn't particularly useful other than in the context of the thread I mentioned above:
(lvalues %hash) = shuffle values %hash;
Updates 1&2: However, note that repeated calls to (lvalues %hash) = values %hash; will only "randomize" the values once, and on older Perls the order won't be very "random" at all apparently not randomize at all, I would guess because the order of the returned values is not randomized between the calls to values. Some interesting reading on the topic: Re^2: setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file and Re^2: Curious: are anon-hashes in random order?
Minor updates for clarification.
|
---|