Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Concerning hash operations (appending, concatenating)

by Zaxo (Archbishop)
on Mar 24, 2005 at 16:31 UTC ( #442103=note: print w/ replies, xml ) Need Help??


in reply to Concerning hash operations (appending, concatenating)

This is one idiom,

%hash = (%hash, e => 'f', g => 'h', i => 'j'); %hash = (%otherhash, %hash);
Order matters; the last instance of a key/value pair is the one that sticks to the key.

After Compline,
Zaxo


Comment on Re: Concerning hash operations (appending, concatenating)
Download Code
Re^2: Concerning hash operations (appending, concatenating)
by thor (Priest) on Mar 24, 2005 at 16:57 UTC
    This is actually really, really useful. People often ask the question "how do I store a value in a hash only if it's a new value?". Here's the answer.

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

      Yeah, but I don't like it. I don't like assigning X to itself like the example does. I'd prefer a slightly long-winded approach:
      $hash{$_} = $otherhash{$_} for grep !exists $hash{$_}, keys %otherhash +;
      Or:
      { my @new_keys = grep !exists $hash{$_}, keys %otherhash; @hash{@new_keys} = @otherhash{@new_keys}; }
      _____________________________________________________
      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
        But isn't that creating arrays and going through loops that are unnecessary?
        --------------
        "But what of all those sweet words you spoke in private?"
        "Oh that's just what we call pillow talk, baby, that's all."
Re^2: Concerning hash operations (appending, concatenating)
by RazorbladeBidet (Friar) on Mar 24, 2005 at 17:55 UTC
    Thanks for that one Zaxo (among others), I hadn't thought of these ; but is there any way to eliminate the redundancy of flattening the hash in
    %hash = (%hash, e => 'f', g => 'h', i => 'j');
    ??
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
Re^2: Concerning hash operations (appending, concatenating)
by ABCXYZ (Novice) on Dec 25, 2012 at 23:06 UTC

    <This is one idiom,

    <%hash = (%hash, e => 'f', g => 'h', i => 'j'); <%hash = (%otherhash, %hash);

    This code reminds me of writing the comma operator in this way: ",=", so to concatenate a hash:

    my %hash1 =qw/ One 1 Two 2 Three 3/; my %hash2 =qw/ Four 4 Five 5 Six 6/; %hash1 ,= %hash2; say keys %hash1;

    And I've checked it, it works in Perl6, the future version of Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://442103]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-10-25 01:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (139 votes), past polls