http://qs321.pair.com?node_id=1129150


in reply to Re^4: Need advice on checking two hashes values and keys
in thread Need advice on checking two hashes values and keys

Okay, if there could be numbers in one file but not the other (I forgot about that in your original data), you won't be able to use the push method, because numbers that weren't in the Spanish file will end up getting the French word in the Spanish spot. So you'll need to do it this way:

$hash{$italian}[1] = $french;

What that does is looks up the array pointed to by the reference in $hash{$italian}, and sets the second element of the array to the value in $french. The cool part is that $hash{$italian} doesn't even have to exist yet; it will be created automatically if it doesn't (that's called autovivification). So numbers that are only in the Spanish file will only have the first element of the array set, and numbers only in the French file will only have the second element set (the first element will be undef). So you'll end up with something like this (I made my own smaller input files to show what I mean):

# Italian -> Spanish input file with 1 & 3 uno = uno tre = tres # Italian -> French input file with 2 & 3 due = deux tre = trois # the hash will be: $hash = ( uno => [ 'uno' ], due => [ undef, 'deux' ], tre => [ 'tres','trois' ], );

See how the missing Spanish word for 2 is undefined, since we inserted the French word into the second element of the array? Now to print them out, you can check each array to see if both elements are present, and print to one file if they are, and the other file if they aren't:

for my $key (keys %hash){ if( $hash{$key}[0] and $hash{$key}[1] ){ # both are present print $out "$key => ", join( ' , ', @{$hash{$key}}), "\n"; } else { # one is missing print $out1 "$key => ", join( ' , ', @{$hash{$key}}), "\n"; } }

Now, in the else portion, you'll get a warning when it prints out an undefined value. You can ignore that, since you're expecting it, or turn off warnings in that section, or deal with it by putting in some "print if it exists" logic. That's up to you, and depends somewhat on what you want that second file to tell you about what's missing.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.