Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Do you know where your variables are?
 
PerlMonks  

Advanced Hash

by qball (Beadle)
on Apr 13, 2001 at 18:29 UTC ( [id://72422]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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 18: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 13, 2001 at 20: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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://72422]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.