One expression, no lingering temp vars. But you're really trying to oversimplify.

my @all_keys = do { my %c = ( %A, %Z ); keys %c };

The keys function wants a single argument that's a just literal hash or array variable of some form (e.g. either a literal %foo or a postderef $bar->%*). It used to at one point be valid to give a scalar to keys and it would (I believe; not 100%) automagically deref. My guess when you tried to give it multiple items keys( %A, %Z ) it's parsing out as list context and you're actually passing it a list of the two hashes' contents unrolled which is why it's triggering the experimental scalar error and the error that you've given it more than one argument.

Edit: Also you might want to take into account the sizes of the hashes involved. It may be much less data copying to iterate over the keys of each candidate hash separately and create a hash from those then take keys of that instead so you're not copying all the values around as well. Which is what the aforementioned uniq is pretty much doing under the hood already (although that's going to build a temporary list of the all the keys to pass as args instead of just iterating over them).

And another thing: If you do make the big überhash be aware that the value is going to correspond to the last key seen from the last hash containing it which you use (e.g. %c = ( %A, %B ) if both have a key "a" the value in $c{a} will have what is in $B{a}).

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re: Problem combining hashes by Fletch
in thread Problem combining hashes by jh

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.