in reply to Union of hashes

If you're happy to create a new hash from the two existing ones, the following should work:

%new_hash=(%a,%b);
I believe that this should work, but test it first:
%a=(%a,%b);
"Livet är hårt" sa bonden.
"Grymt" sa grisen...

Replies are listed 'Best First'.
Re: Re: Union of hashes
by gellyfish (Monsignor) on Feb 07, 2002 at 12:00 UTC

    Or, for no particular reason that I can think of :

    @a{keys %b} = values %b;

    Assuming that keys and values are guaranteed to return in the same order :)

    /J\

      They are guaranteed to be in the same order. Note though that in both your solution and Biker's solution this is a union of keys. Entries in %b will overwrite entries in %a with their values when they share the same key. There's really no way around this, and it's probably what IvorW wants, but I thought I'd mention it for safety's sake.
        I should point out that while it's simple to redefine the behavior to have values of %a overwrite %b, or vice verse, you can get more customizable behavior using *cough*my module*cough* Hash::Merge. One included way with this module would have values of equivalent keys to be placed in an anon array, but the module allows for specific settings.

        -----------------------------------------------------
        Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
        "I can see my house from here!"
        It's not what you know, but knowing how to find it if you don't know that's important