Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Comparing 2 hash tables where values are stored in arrays

by Laurent_R (Canon)
on Sep 05, 2014 at 21:31 UTC ( #1099732=note: print w/replies, xml ) Need Help??


in reply to Comparing 2 hash tables where values are stored in arrays

The module suggested by AppleFritter is certainly the easiest way.

But in case you want to do it yourself in pure Perl without using a module, the most idiomatic way to do it is probably to use a hash (or two hashes in this case). This command-line script (re-using AppleFritter sample data) should give you a gist on how to do it:

$ perl -E ' my %hash1 = ("abc" => [1, 2, 3, 4]); my %hash2 = ("abc" => [1, 3, 5, 7]); my %val1 = map {$_, 1} @{$hash1{abc}}; my %val2 = map {$_, 1} @{$hash2{abc}}; say "Values missing in hash2: ", join " ", grep {not exists $val2{$_}} + keys %val1; say "Values missing in hash1: ", join " ", grep {not exists $val1{$_}} + keys %val2; ' Values missing in hash2: 4 2 Values missing in hash1: 7 5
Please ask if there is something you don't understand.

Edit: I had not seen j0se's solution when I suggested mine. Although the two solutions look very different, they are based on exactly the same ideas. I probably would not have posted anything if I had seen j0se's post before. Now it is done, I leave the post, it shows at least that, really, TIMTOWTDI in Perl.

(BTW, j0se, I have spent at least 18 weeks in your country, specifically in Bratislava, working for Orange Slovakia back in 2008 (preparing the Euro transition), and also 6 or 7 more weeks a few years before. And, long before, I also visited Bratislava as a young tourist back in 1975, in a totally different and quite difficult political context. Nice city and very nice people. Sorry for the off-topic digression.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1099732]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2022-08-14 01:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?