Just another Perl shrine | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
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:
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.
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:
Updates 1&2: However, note that repeated calls to (lvalues %hash) = values %hash; will Minor updates for clarification. In reply to Assigning to Hash Values (updated) by haukex
|
|