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.
|
|---|