Beefy Boxes and Bandwidth Generously Provided by pair Networks
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:

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.


In reply to Assigning to Hash Values (updated) by haukex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found