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.