Hello Nansh,
You need to show us at least that you tried to find a solution to your problem.
Your code does is not even correctly defined:
'Fruits'=> 'Apple', 'Orange', 'Banana', 'Grapes', , 'Fruits1'=> 'Apples', 'Orange', 'pineapple', 'Grapes',
When it should be like this:
#!/usr/bin/perl use strict; use warnings; my %HoA1 = ( Fruits => [ "Apple", "Orange", "Banana", "Grapes" ], ); my %HoA2 = ( Fruits1 => [ "Apples", "Orange", "pineapple", "Grapes" ], );
Help us to help you, read (How do I post a question effectively?)
A good starting point could be (Test::More):
#!/usr/bin/perl use strict; use warnings; use Test::More tests => 1; my %HoA1 = ( Fruits => [ "Apple", "Orange", "Banana", "Grapes" ], ); my %HoA2 = ( Fruits1 => [ "Apples", "Orange", "pineapple", "Grapes" ], ); is_deeply(\%HoA1, \%HoA2, 'data structures should be the same'); __END__ $ perl test.pl 1..1 not ok 1 - data structures should be the same # Failed test 'data structures should be the same' # at test.pl line 14. # Structures begin differing at: # $got->{Fruits1} = Does not exist # $expected->{Fruits1} = ARRAY(0xdc0bc8) # Looks like you failed 1 test of 1.
If you modify the keys to be the same e.g. "Fruits1" to "Fruits":
#!/usr/bin/perl use strict; use warnings; use Test::More tests => 1; my %HoA1 = ( Fruits => [ "Apple", "Orange", "Banana", "Grapes" ], ); my %HoA2 = ( Fruits => [ "Apples", "Orange", "pineapple", "Grapes" ], ); is_deeply(\%HoA1, \%HoA2, 'data structures should be the same'); __END__ $ perl test.pl 1..1 not ok 1 - data structures should be the same # Failed test 'data structures should be the same' # at test.pl line 14. # Structures begin differing at: # $got->{Fruits}[0] = 'Apple' # $expected->{Fruits}[0] = 'Apples' # Looks like you failed 1 test of 1.
Update: Based on your title of your question "Findidng Difference between two hash of arrays" you mean that you have two different hashes that contain array(s) inside. But based on your update of your question you probably mean that you have one hash with two arrays inside. This is why you should show us sample of your code and what you have tried so far.
Having said that sample of solution:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Array::Utils qw(:all); my %HoA = ( Fruits => [ "Apple", "Orange", "Banana", "Grapes" ], Fruits1 => [ "Apples", "Orange", "pineapple", "Grapes" ], ); # print Dumper \%HoA; =sample of Hash of Arrays output $VAR1 = { 'Fruits1' => [ 'Apples', 'Orange', 'pineapple', 'Grapes' ], 'Fruits' => [ 'Apple', 'Orange', 'Banana', 'Grapes' ] }; =cut my @AoAValues = sort values %HoA; # print Dumper \@AoAValues; =sample of Array of Arrays from values $VAR1 = [ [ 'Apple', 'Orange', 'Banana', 'Grapes' ], [ 'Apples', 'Orange', 'pineapple', 'Grapes' ] ]; =cut # get items from array @{$AoAValues[1]} that are not in array @{$AoAVa +lues[0]} # my @diff = array_minus( @{$AoAValues[1]}, @{$AoAValues[0]} ); my %hash; $hash{'difference'} = [ array_minus( @{$AoAValues[1]}, @{$AoAValues[0] +} ) ]; print Dumper \%hash; __END__ $VAR1 = { 'difference' => [ 'Apples', 'pineapple' ] };
I used Array::Utils module to compare the arrays, it has also some other features that you might be interested.
Update2: In case that you where trying to compare hashes of arrays as my initial impression try something like this:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Array::Utils qw(:all); my %HoA1 = ( Fruits => [ "Apple", "Orange", "Banana", "Grapes" ], Fruits1 => [ "WaterMellon" ], ); my %HoA2 = ( Fruits => [ "Apples", "Orange", "pineapple", "Grapes" ], Fruits1 => [ "Mellon" ], ); my @AoAValues1 = sort values %HoA1; my @AoAValues2 = sort values %HoA2; my %hash; for (0 .. $#AoAValues1) { $hash{$_} = [ array_minus( @{$AoAValues2[$_]}, @{$AoAValues1[$_]} +) ]; } print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { '0' => [ 'Apples', 'pineapple' ], '1' => [ 'Mellon'
Hope this helps.
In reply to Re: Findidng Difference between two hash of arrays
by thanos1983
in thread Findidng Difference between two hash of arrays
by Nansh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |