Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Syntax explanation required

by LanX (Canon)
on Jan 02, 2013 at 12:35 UTC ( #1011259=note: print w/ replies, xml ) Need Help??


in reply to Syntax explanation required

> 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")

Cheers Rolf

UPDATES:

╣) because repeated keys will be overwritten.

▓) at least with older perl-versions, see Athanasius' remark


Comment on Re: Syntax explanation required
Select or Download Code
Replies are listed 'Best First'.
Re^2: Syntax explanation required
by ghosh123 (Monk) on Jan 02, 2013 at 13:00 UTC
    Wonderful explanation by 'step-by-step'

    Thanks a lot !!!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2015-07-08 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (94 votes), past polls