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
Re: hash keys compare
by Joost (Canon) on Dec 18, 2007 at 15:04 UTC
|
anonymonk, please read writeup formatting tips the next time you post. We don't use bbcode type formatting here.
To answer your question, you can use exists:
for my $key (keys %{$hash->{$a}{$b}}) {
unless (exists $hash2->{$d}{$e}{$key}) {
die "Key $key not found in hash2";
}
}
If this is not what you mean, you'd probably have to explain why $c and $f are not the same value in your post, and how $a,$b,$d and $e are determined.
| [reply] [d/l] |
Re: hash keys compare
by Zaxo (Archbishop) on Dec 19, 2007 at 08:54 UTC
|
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.
| [reply] [d/l] |
|
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
| [reply] |
|
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. | [reply] [d/l] |
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
| [reply] [d/l] |
|
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?
| [reply] |
|
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.)
| [reply] [d/l] [select] |
|
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: | [reply] [d/l] |
|
|