in reply to Common elements of a hash of hashes

#!/usr/bin/perl use strict; use warnings; my %HoH = ( elt1 => { A => "ccc", B => "ccc", C => "ccc", }, elt2 => { A => "ccc", C => "ccc", D => "ccc", B => "ccc", }, elt3 => { A => "ccc", E => "ccc", C => "ccc", }, ); my %count; my $count = keys %HoH; while (my ($name, $hash) = each %HoH) { while (my ($key) = each %$hash) { $count{$key}++; } } while (my ($name, $hash) = each %HoH) { while (my ($key) = each %$hash) { delete $$hash{$key} unless $count{$key} == $count; } } use Data::Dumper; print Dumper \%HoH; __END__ $VAR1 = { 'elt2' => { 'A' => 'ccc', 'C' => 'ccc' }, 'elt3' => { 'A' => 'ccc', 'C' => 'ccc' }, 'elt1' => { 'A' => 'ccc', 'C' => 'ccc' } };
Perl --((8:>*

Replies are listed 'Best First'.
Re^2: Common elements of a hash of hashes
by Skeeve (Parson) on Oct 07, 2005 at 09:29 UTC

    Almost exactly what I wrote...

    How do you like this version? ;-)
    use strict; use warnings; my %HoH = ( elt1 => { A => "ccc", B => "ccc", C => "ccc", }, elt2 => { A => "ccc", C => "ccc", D => "ccc", B => "ccc", }, elt3 => { A => "ccc", E => "ccc", C => "ccc", }, ); my %count; my $count= scalar keys %HoH; for ( [ grep { $count!=$count{$_} } map { ++$count{$_};$_ } map { keys + %{$HoH{$_}} } keys %HoH ] ) { foreach my $hash (values %HoH) { delete(@$hash{@$_}); } } use Data::Dumper; print Dumper(\%HoH);

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print

      How do you like this version?

      Not obscure enough for obfuscated code, and too obscure for normal code. I never liked for (SCALAR) {... $_ ...} as an alternative for {my $var = SCALAR; ... $var ...}.

      Perl --((8:>*