Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Syntax explanation required

by eyepopslikeamosquito (Canon)
on Jan 03, 2013 at 01:27 UTC ( #1011379=note: print w/ replies, xml ) Need Help??


in reply to Syntax explanation required

Two alternative ways derived from: perldoc -q duplicate

my %seen; my @uniq = grep { !$seen{$_}++ } keys(%hash1), keys(%hash2);

use List::MoreUtils qw(uniq); my @uniq = uniq( keys(%hash1), keys(%hash2) );


Comment on Re: Syntax explanation required
Select or Download Code
Re^2: Syntax explanation required (uniq)
by LanX (Canon) on Jan 03, 2013 at 01:58 UTC
    I was meditating about advising List::MoreUtils:uniq as a best practice, cause I thought it prevents any stringification problems.

    Unfortunately it doesn't! :(

    DB<125> $a=["A"] => ["A"] DB<126> $b=["B"] => ["B"] DB<127> @a=($a,"$a",$b,"$b") => (["A"], "ARRAY(0x96cb0f0)", ["B"], "ARRAY(0x96cfd80)") DB<128> @b= uniq @a => (["A"], ["B"])

    thats quite disappointing for an XS-module ...

    EDIT: Well at least it preserves order...

    Cheers Rolf

    ) I should explain, the problem with using hashes for set operations is that keys are stringified, that means working with anything which isn't a string or at least a scalar comes with a risk.

    Practically it's not possible to safely use a ref as a key because of the resulting string is irreversible.

    DB<130> $h{$a}=5 => 5 DB<131> %h => ("ARRAY(0x96d1f00)", 5)

    UPDATE:

    improved code example...

      Thank you all. Very helpful !!!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (16)
As of 2014-07-23 14:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (145 votes), past polls