Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

hash keys compare

by Anonymous Monk
on Dec 18, 2007 at 14:54 UTC ( [id://657685]=perlquestion: print w/replies, xml ) Need Help??

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

I have two hashes :
$hash1->{$a}{$b}{$c} $hash2->{$d}{$e}{$f}
How do make sure that the key, $f of hash2 is present in $c of hash1. Like wise how do I make sure that keys, $c of hash1 are present in $f of hash2. I basically was a list of $c (keys) of hash1 that are missing in $f (keys) of hash2 and $f of hash2 that are missing in $c of of hash1 Thank you

Replies are listed 'Best First'.
Re: hash keys compare
by Joost (Canon) on Dec 18, 2007 at 15:04 UTC
Re: hash keys compare
by Zaxo (Archbishop) on Dec 19, 2007 at 08:54 UTC

    Do the parent keys have to match?

    Otherwise,

    my @unseen = do { delete %{$hash1->{$a}{$b}}, keys %{$hash2->{$d}{$e}}; keys %{$hash1->{$a}{$b}}; }
    That leaves it up to you to set up the parent keys you want in the match. That's destructive of $hash1.

    $a and $b are poor choices for variable names. They are sacred to sort and can have side effects.

    After Compline,
    Zaxo

      hi Zaxo, What you have done makes sense, except that I dont want to delete but I want to print the variables. I just want to be able to print it . I am also getting this error when I use the code. html delete argument is not a HASH or ARRAY element or slice at db2cfg-report line 394. /html
      if you do not want to alter the original hashes, then you must make copies of them before deleting. e.g.:

      > perl -wMstrict -e "my $hashrefA = {}; my $hashrefB = {}; my ($p, $q, $r, $s) = qw(fee fie foe fum); $hashrefA->{$p}{$q} = { qw(a 1 b 2 x 8 y 9) }; $hashrefB->{$r}{$s} = { qw(c 3 d 4 x 6 y 7) }; my %Apq_not_in_Brs = %{ $hashrefA->{$p}{$q} }; delete @Apq_not_in_Brs{ keys %{ $hashrefB->{$r}{fum} } }; my %Brs_not_in_Apq = %{ $hashrefB->{$r}{$s} }; delete @Brs_not_in_Apq{ keys %{ $hashrefA->{fee}{$q} } }; print qq(Apq not in Brs: @{[ keys %Apq_not_in_Brs ]} \n); print qq(Brs not in Apq: @{[ keys %Brs_not_in_Apq ]} \n); " Apq not in Brs: a b Brs not in Apq: c d

      also: please give the monks a break and read and follow the Writeup Formatting Tips; the markup you're using is not doing what you seem to expect.

Re: hash keys compare
by Anonymous Monk on Dec 18, 2007 at 15:36 UTC
    i'm not sure i understand the question, but if i do, here's a possible answer:

    perl -wMstrict -e "my %X = qw(a 1 b 2 c 3 z 9); my %Y = qw(d 4 e 5 f 6 z 8); my %X_not_in_Y = %X; my %Y_not_in_X = %Y; delete @X_not_in_Y{ keys %Y }; delete @Y_not_in_X{ keys %X }; print qq(X not in Y: @{[ keys %X_not_in_Y ]} \n); print qq(Y not in X: @{[ keys %Y_not_in_X ]})" X not in Y: c a b Y not in X: e d f
      hi Super doc, What I am trying to explain is that the list of keys in $c in hash1 should be exactly similar to list of keys in $f in hash2. I just want a list of keys that are not present in either of them. For ex: html $a = 'a, b , c , d, e, f'; $f = 'a, 1, c, d, 2, , f'; SO I want a report saying : $a is missing keys "1, 2". $f is missing keys "b, e". /html The hash that you used to explain works fine for simple hash tables with one key and one value. I am not sure how to build a data structure, for a hash like this one: html $hash1->{$a}{$b}{$c} $hash2->{$d}{$e}{$f} /html Can you help me out?
        oops... created a little too soon -- it's late.

        I meant to say, See:   perlref   perlreftut   perldsc   to get you started.

        (by the way -- the output of the print statement in the code above is fum.)

        I believe you are laboring under a misapprehension (or else I am).

        You write of a "... list of keys in $f" and then give an example. Your example is not of a list, but of a string literal assigned to a scalar. A string literal can be used as a single key in a hash. Likewise, a scalar holding a string can be used as a single key in a hash. The two string literals given in your example are not the same, so they can be used as two keys in the same hash.

        It is possible to ask questions about how similar or different two strings are and get useful answers, but you must first be very clear about just what similar and different mean in the context of your problem.

        On The Other Hand...

        If you just want to construct a nested data structure using hashes, you are almost there with the other example you give.

        my $hash_reference = {}; # empty hash ref for now my $a = 'fee'; my $b = 'fie'; my $c = 'foe'; $hash_reference->{$a}{$b}{$c} = 'fum'; # same as... $hash_reference->{'fee'}{'fie'}{'foe'} = 'fum'; # same as... $hash_reference->{fee}{fie}{foe} = 'fum'; print $hash_reference->{fee}{fie}{foe};

        See:

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-12-08 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (51 votes). Check out past polls.