in reply to Comparing hash data

If you just want to compare whether the values of each hash are the same you could do :
foreach my $v1 (values %sql) { foreach my $v2 (values %csv) { if ($v1 ne $v2) { # do something } } }
There is probably a more efficient way, but I think this would do the trick.

EDIT: Thanks to cLive for pointing out that I'm totally on crack. Look at his post below for the good way to do this.

Replies are listed 'Best First'.
Re^2: Comparing hash data
by cLive ;-) (Prior) on Oct 01, 2004 at 20:12 UTC
    This is wrong. Very wrong. Testing each value against every other value in the other hash? So, for hashes with n keys, you're going to have at least (n-1)^2 cases where $v1 ne $v2 - and that's if they match. NAME may not match EMP_NAME, but it definitely won't match EMP_PHONE, EMP_AGE etc, so why test it?

    Only n matches need to be made - assuming you can map the keys across the hashes correctly (my guess is below on this one).

    I think you misunderstood the question.

    cLive ;-)

      You are totally right I am not sure what I was thinking when I wrote that. When I saw your post I smashed my head against the keyboard. I've had a long day...

      correct.. but without making assumptions about the "beginning" of a key we can't truly know how to properly map key to key. I agree it's inefficient.. however it was the only way I saw (well agreed with actually) to do it without making assumptions.

        Even if you don't know the beginning of the key, this way is completely useless at trying to map values to values even.

        It's testing every value against every value, with no references to keys, so how on earth are you going to be able to tell which keys are csv/sql pairs from this?

        My quick hack solution assumes you know the keys. Even if you don't, it would be trivial use a regular expression to map them across.

        However you do it though, the point is to check the corresponding values of key pairs, not compare one value against all possible values in the other hash.

        cLive ;-)

Re^2: Comparing hash data
by Grygonos (Chaplain) on Oct 01, 2004 at 20:07 UTC

    That's true you could do values and sort on values and report any mismatches., and their field names. of course you may run into a cascading problem.. if field 2 doesn't match..then depending on what's causing the problem.. the rest of them may throw errors due to improper ordering.. or they may not. This is a decent way without fixing your data struct.. however that's still my reccomendation