> my @uniq = keys %{ {%hash1, %hash2} };
From inside out:
- First the two hashes are flattened to lists, which are combined to a long list.
- Building a new (anonymous) hashref will eliminate duplicated entries from that hash ¹
- To be able to extract² the keys you still need to dereference this ano-hashref with %{..}.
step by step:
DB<111> %hash1= (a=>1,b=>2)
=> ("a", 1, "b", 2)
DB<112> %hash2= (a=>1,c=>3)
=> ("a", 1, "c", 3)
DB<113> (%hash1, %hash2)
=> ("a", 1, "b", 2, "c", 3, "a", 1)
DB<115> + {%hash1, %hash2}
=> { a => 1, b => 2, c => 3 }
DB<117> %{ {%hash1, %hash2} }
=> ("c", 3, "a", 1, "b", 2)
DB<118> keys %{ {%hash1, %hash2} }
=> ("c", "a", "b")
UPDATES:
¹) because repeated keys will be overwritten.
²) at least with older perl-versions, see Athanasius' remark
-
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.