Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Monks
I created two HoHs and I am trying to compare the inner keys from %hash2 with the inner keys of %hash1 for each of its outer key (hash1), to calculate some score-p (from p0001, p-0002, etc) by adding the value from each logt (A, B, etc), when found; otherwise add $delta value to score. This score-p must be store in another hash

e.g. Score of p0001 compare to (A)= 0.0163934+0.0163934+$delta+$delta...
Score of p0001 compare to (B)= 0.0163934+$delta+$delta+$delta..
e.g. Score of p0002 compare to (A)= 0.0163934+0.0163934...
Score of p0002 compare to (B)= 0.0163934+0.0163934
my %hash1 = ( 'A' => { 'log1' => [ '1', '0.0163934' ], log2' => ['1','0.0163934'], 'log3' => ['1','0.0163934' ], } 'B' => { 'log1' => [ '1', '0.0134521' ], 'log3' => ['2','0.0244508'], } ); my %hash2 = ( 'p0001' => { 'log1' => '1', 'log2' => '3', 'log5' => '1', 'log8' => '1',... } 'p0002' => { 'log1' => '1', 'log3' => '2',... } p0004 => ..., p0056 => ..., );
My problem is how to compare all inner keys of each p (%hash2) with all inner key of all logt (%hash1). This is what I have so far...I am stuck!
$hash1{$logt}{$keylogs}[1]; $hash2{$p}{$pkeylogs}; $hash3{$p} {$logt} =>$totalscore; foreach my $keylogt (keys(%hash1)){ foreach my $keyp (keys(%hash2)){ foreach my $keypinner (keys %{$hash2{$keyp}}){ foreach my $keylogtinner (keys %{$hash1{$keylogt}}){ until($keypinner eq $keylogtinner){ ... $totalscore+=$hash1{$keylogt}{$keylogtinner}[1]; } else{ $totalscore+=$delta; } } } } }
Please I need some guidance
Thanks

Replies are listed 'Best First'.
Re: How to compare inner keys from two Hashes
by matija (Priest) on Mar 30, 2004 at 09:12 UTC
    How about this:
    foreach my $keylogt (keys(%hash1)){ foreach my $keyp (keys(%hash2)){ my @allkeys=(keys %{$hash2{$keyp}},keys %{$hash1{$keylogt}}); my %unique; map($unique{$_}++,@allkeys); my @uniquekeys=keys %unique; # now you have a list of ALL keys in both hashes # and you can process each one and determine if # it appears in one, the other, or both.. } }
      Thank you Matija for answering me
      I am new on this, and I do not quite understand how/where in your code can I distinguish keys from %hash1 and %hash2, and process them individually?
      Does @uniquesKey contain keys that did not have a match? but from both hashes?
      I am still confused, how will this solve my problem?
      Thanks
Re: How to compare inner keys from two Hashes
by Roy Johnson (Monsignor) on Mar 30, 2004 at 19:54 UTC
    Maybe this will be something in a "teach you how to fish" vein:
    You can (obviously) get all the keys of %hash1 with keys %hash1, and all the values with values %hash1.

    Now, each of those values is, itself, a hash reference, and you can get the keys of any reference by doing keys %{$reference} (or similarly with values). In fact, you can operate on %{$reference} exactly like you would on any other hash. (The braces are optional when the reference is a simple scalar variable.)

    If you want to get all the "logn" type keys out of your %hash1, you could do it something like this:

    my @h1_inners = (); for my $ref (values %hash1) { push(@h1_inners, keys %$ref); }
    and similarly for %hash2. I don't really understand your explanation of your goal, so I have to bug out here, but I hope this helps.

    The PerlMonk tr/// Advocate