Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Difference of Hash

by Nansh (Acolyte)
on Apr 06, 2017 at 09:54 UTC ( [id://1187237]=perlquestion: print w/replies, xml ) Need Help??

Nansh has asked for the wisdom of the Perl Monks concerning the following question:

I have an array

@arr= ("Fruit","Fruit1","Fruit3","Fruit4")

@arr1=("car","car1","car2","car3","car4")

My code is like this

#usr/bin/perl use Data::Dumper qw(Dumper); foreach $x(@arr) { I will do something and i will put something into an array called @v +al for each of the $x $hash{$x}=[@val]; } print Dumper \%hash; foreach $y(@arr1) { I will do something and i will put something into an array called @v +al2 for each of the $y $hash2{$x}=[@val2]; } print Dumper \%hash2; my %result; foreach $key(keys %hash1) { if(!exists $hash{$key}) { $result{$a}=$hash1{$key}; next; } foreach $new(keys %$key) { $result{$key}{$new}=$hash{$key}{$new} if(!exists $hash{$key}{$new} & +& $hash1{$key}{$new}) eq $hash{$key}{$new}); } } print Dumper \%result;

From above code i am not able to find the difference between two hash

I need to find the difference that is if the element is missing or element is extra in %hash1 it should show which element is missing or which element is extra when compared with %hash

It should check for both keys and values of the hash

please kindly help me to solve this i am strucked here

Thanks in advance

Replies are listed 'Best First'.
Re: Difference of Hash
by Eily (Monsignor) on Apr 06, 2017 at 10:12 UTC

    There are many cases where you use a variable that's not defined, out of scope, or not what you think ($key is not a hashref). Luckily you can find all those mistakes by applying three simple rules:

    • Add use strict; and use warnings; at the top of your file. Perl will now tell you about those mistakes.
    • Declare your variable with my. This way perl can tell when you've made a typo in a variable's name.
    • Avoid single letter names, especially $a, which has a special value (some built-ins may use and change it (like sort), and perl won't warn you if you forgot to declare it). $x and $y may be an exception because they are pretty explicit, but only when they are used as coordinates.

Re: Difference of Hash
by huck (Prior) on Apr 06, 2017 at 10:22 UTC

    if you add use strict; use warnings; i bet you get enough clues to figure it out yourself!

    hint %$key

Re: Difference of Hash (reposts updated)
by LanX (Saint) on Apr 06, 2017 at 10:17 UTC
Re: Difference of Hash
by Discipulus (Canon) on Apr 06, 2017 at 10:24 UTC
Re: Difference of Hash
by thanos1983 (Parson) on Apr 06, 2017 at 11:08 UTC

    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: perlquestion [id://1187237]
Approved by Eily
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-05-26 18:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found