This all comes back to the error message:

Type of arg 1 to keys must be hash or array (not list) at - line 3, ne +ar "%Z)"

keys() really wants a hash as its single argument. Not a list. And %A, %Z is a list. Admittedly, it is a list of pairs that can again be interpreted as a hash, but keys() wants to have that hash.

Using +{ %A, %Z }->%* or %{{ %A, %Z }} constructs the list of pairs from the two hashes, wraps these in a hash reference and then dereferences that hash reference, giving the hash to keys(). This is why that one works.

Your other approaches fail because keys() only takes a single argument, and that argument must be a hash. Everything you try to pass to it is a list, because the comma operator "," creates a list or the assignments go through an intermediate list (and don't return the assigned hash).

If you are only interested in getting a unique list of keys, you could use List::Utils uniq, but that is not shorter than the two step approach, especially if you also later want to look at the combined values:

my @all_keys = uniq( keys(%A), keys(%Z) );

In reply to Re: Problem combining hashes by Corion
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.