in reply to Hash key intersection

my @A{ qw[ 1 3 5 6 ] } = (); my @B{ qw[ 1 2 3 4 6 7 ] } = (); print join'.', ( exists $A{ $_ } ? 'A:' . $_ : ' ' ), ( exists $B{ $_ } ? 'B:' . $_ : ' ' ) for sort keys %{ { %A, %B } }; A:1.B:1 .B:2 A:3.B:3 .B:4 A:5. A:6.B:6 .B:7

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!

Replies are listed 'Best First'.
•Re: Re: A man walks into a bar with a hash under each arm!
by merlyn (Sage) on Dec 30, 2003 at 20:00 UTC
    sort keys %{ { %A, %B } }
    You'll get some spurious responses if %A and %B have values there, because you're mixing keys and values into a keys pile. Perhaps you wanted to add keys in front of each of the hashnames?

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.


    update: OK, yeah, totally off base on this one. What was I thinking? {grin}

      Actually, it's important that you don't use keys in front of the individual hashes, otherwise alternate keys get used as values in the construction of the anonymous hash.

      As I as showed, but with values added you get

      @A{ qw[ 1 2 4 6 ] } = qw[ a b d f ]; @B{ qw[ 1 2 3 4 6 7 ] } = qw[ a b c d f g ]; print join '.', ( exists $A{ $_ } ? 'A:' . $_ : ' ' ), ( exists $B{ $_ } ? 'B:' . $_ : ' ' ) for sort keys %{ { %A, %B } }; A:1.B:1 A:2.B:2 .B:3 A:4.B:4 A:6.B:6 .B:7

      But using the extra key statements you get

      print join'.', ( exists $A{ $_ } ? 'A:' . $_ : ' ' ), ( exists $B{ $_ } ? 'B:' . $_ : ' ' ) for sort keys %{ { keys %A, keys %B } }; A:1.B:1 A:6.B:6 .B:7

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      Hooray!