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

Comparing two hashes is simple:
for $comp_name (keys %CompHashDB) { print "DIFF: $comp_name\n" if $CompHashDB{$comp_name} ne $hashCompan +y{$comp_name}; }

Now I want to compare a key/value hash with a hash of arrays. Here's how the hashes are formed:
@{$hashCompany{$vars[1]}} = join(',',@vars[0,1]);

and
$CompHashDB{$data[0]} = "$data[0]";

How would I compare the keys and print the diffs of hashCompany with @vars[0,1]?

qball~"I have node idea?!"

Replies are listed 'Best First'.
Re: Advanced Hash
by Russ (Deacon) on Apr 13, 2001 at 22:54 UTC
    Well, let me address a couple points first.
    @{$hashCompany{$vars[1]}} = join(',',@vars[0,1]);
    This isn't a hash of arrays. It is just a hash of scalars. You don't need to use @{} because join(',',@vars[0,1]) is not a list; it is just a string.

    You don't need to make perl interpolate $data[0] - just drop the quotes.

    So, since we are only comparing two hashes, you have already mentioned one technique. Check out Re: diff of two hashes. for other ways to compare hashes (show keys in first hash that are not in second, show keys in second hash that are not in first).

    Russ
    Brainbench 'Most Valuable Professional' for Perl

Re: Advanced Hash
by knobunc (Pilgrim) on Apr 14, 2001 at 00:24 UTC

    As Russ pointed out, you are not making an array in the second case. However, you are also not comparing the hashes correctly in the first case since the %hashCompany might have additional stuff that you will not see. So:

    my %hash1 = {a => 1, b => 2}; my %hash2 = {a => 1, b => 2, c => 3};

    would pass your check above. You can quickly check that with keys() to see how big the hashes are and shortcircuit if they are different sizes.

    Not that this helps answer your real question. I would try but I am not yet sure what you want your data structure to look like.

    -ben