in reply to working with multiple hash refs

As sauoq said, the question is ambiguous, because you haven't said how you want to handle the case where the same key appears under both D and E -- and your sample hash presents exactly that situation!
But assuming you want to print out all the data, regardless of such duplication,
what I would do is make a new hash that essentially inverts the lowest two levels of the given hash. Like so:
my %h; # the inverted hash for my $de ( keys %{ $hash{A}{B}{C} } ) { # gives D, E for my $k ( keys %{ $hash{A}{B}{C}{$de} } ) { $h{$k}{$de} = $hash{A}{B}{C}{$de}{$k}; } } # now print it out: for my $k ( sort keys %h ) { for my $de ( sort keys %{$h{$k}} ) { print "$k = $h{$k}{$de} (from $de)\n"; } }

jdporter
...porque es dificil estar guapo y blanco.

Replies are listed 'Best First'.
Re: Re: working with multiple hash refs
by sauoq (Abbot) on Dec 19, 2002 at 22:18 UTC

    That is very broken. If a value appears more than once in the two hashes, then your code will discard all but one of the keys it is associated with. You can't just invert the hashes unless you know that all of the values are unique (or you don't care about losing data.)

    Update: I was wrong. As jdporter explains below, he is not using values as keys. My apologies.

    -sauoq
    "My two cents aren't worth a dime.";
    
      (Somehow I don't feel that posting nodes in a thread is the best way to have this kind of debate, but...)

      I don't know how to tell you gently that you are quite mistaken.
      I am only inverting two levels of keys of hashes. The values stay at the "leafs", where they were.

      In other words, given hash %a as     $a{foo}{bar} = value; I am creating an "inversion" of that hash, %b, as     $b{bar}{foo} = value; No potential for loss. No values being used as keys, nor vice versa.

      jdporter
      ...porque es dificil estar guapo y blanco.

        I don't know how to tell you gently that you are quite mistaken.

        There's no need to tell me gently. You are quite right. I was mistaken; your solution is fine. I did not thoroughly read your code. Please accept my apologies.

        -sauoq
        "My two cents aren't worth a dime.";