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

Re^3: Common hash keys

by parv (Parson)
on Jun 07, 2008 at 08:34 UTC ( [id://690810]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Common hash keys
in thread Common hash keys

(: Indeed! (But I did not write Perl, did I?)

Replies are listed 'Best First'.
Re^4: Common hash keys
by massa (Hermit) on Jun 07, 2008 at 13:34 UTC
    Maybe
    my @keys_in_commmon = grep $hashref1->{$_}, keys %$hashref2;
    would've sufficed?? If you just have to determine if they have keys in common,
    if( grep $hashref1->{$_}, keys %$hashref2 ) { ... }
    would do the trick...
      I would say  my @keys_in_commmon = grepexists($hashref1->{$_}), keys %$hashref2; to avoid autovivification.

        massa's code does not actually cause autovivification (perl 5.8.8) ...

        use warnings; use strict; use Data::Dumper; my %p; my %q = ( 1 => undef ); print Dumper( 'before', \%p ); my $r = grep $p{ $_ }, keys %q; print Dumper( 'after', \%p );

        ... I was also suspicious of autovivification but had to test that myself.

        The exists test is so that the case where a key has undef as a value, not to aviode auto vivification. Undef itself cannot be a key in a hash. But '0' and '' can.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found