Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Difference of Hash

by thanos1983 (Parson)
on Apr 06, 2017 at 11:08 UTC ( [id://1187254]=note: print w/replies, xml ) Need Help??


in reply to Difference of Hash

Hello Nansh,

It seems that you are doing your first steps in Perl just now. It would be very good for you to always use strict and warnings.

Now regarding your question, since I got the opportunity to answer on your previous question (Findidng Difference between two hash of arrays), I start to wonder if you really want to use Hashes of Arrays or simple perldata (hash/Subscripts).

When you have a hash you do care about the sequence of the keys in the hash, as long as you have the key you can compare keys. I think in you case you want to compare the fruits and find the difference. If this is the scenario (assuming because your description and approach is not clear) it can be done like this:

#!/usr/bin/perl use warnings; use strict; use Hash::Diff qw( diff ); use Data::Dumper qw(Dumper); my %hash; my @fruits = ("Fruit","Fruit1","Fruit3","Fruit4"); my @fruit_names = ("apple", "orange", "strawberry", "grape"); my @fruits_2 = ("Fruit","Fruit1","Fruit3","Fruit4"); my @fruit_names_2 = ("apple", "orange", "strawberry", "grapes"); # Approach one creating hash with 2 arrays for (0..$#fruits) { $hash{$fruits[$_]} = $fruit_names[$_]; } # print Dumper \%hash; # Approach two creating hash with 2 arrays my %hash_2; @hash_2{@fruits} = @fruit_names; # print Dumper \%hash_2; my %hash_3; @hash_3{@fruits_2} = @fruit_names_2; # print Dumper \%hash_3; my %hash_diff = %{ diff( \%hash_3, \%hash_2 ) }; print Dumper \%hash_diff; __END__ $ perl test.pl $VAR1 = { 'Fruit4' => 'grapes' };

Obviously the two hashes contain the same names and fruits apart from the plural on the second hash.

I hope this helps more.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2025-07-19 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.