Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Findidng Difference between two hash of arrays

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

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

Hello i need to find the difference between two hash of arrays.

My hash looks like this

'Fruits'=>[ 'Apple', 'Orange', 'Banana', 'Grapes', ], 'Fruits1'=>[ 'Apples', 'Orange', 'pineapple', 'Grapes', ]

I need output like this

'difference'=>[ 'Apples', 'pineapple', ]

I couldnt solve this please help me Thanks in advance

Replies are listed 'Best First'.
Re: Findidng Difference between two hash of arrays
by Corion (Patriarch) on Apr 04, 2017 at 07:49 UTC

    This is a FAQ. See perlfaq4 on "difference".

Re: Findidng Difference between two hash of arrays
by thanos1983 (Parson) on Apr 04, 2017 at 08:11 UTC

    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:

    When it should be like this:

    Help us to help you, read (How do I post a question effectively?)

    A good starting point could be (Test::More):

    If you modify the keys to be the same e.g. "Fruits1" to "Fruits":

    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:

    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:

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      "Orange you glad I did not say 'banana?'"
Re: Findidng Difference between two hash of arrays
by BillKSmith (Monsignor) on Apr 04, 2017 at 15:16 UTC
    This is a common set operation. You may prefer to use a module.
    use strict; use warnings; use Set::Array; use Data::Dumper; my %HoA = ( 'Fruits' => [ 'Apple', 'Orange', 'Banana', 'Grapes', ], 'Fruits1' => [ 'Apples', 'Orange', 'pineapple', 'Grapes', ] ); my $set1 = Set::Array->new(@{$HoA{Fruits1}}); my $set = Set::Array->new(@{$HoA{Fruits }}); $HoA{difference} = [Set::Array::difference($set1, $set, 0)]; print Dumper(\%HoA); OUTPUT: $VAR1 = { 'Fruits1' => [ 'Apples', 'Orange', 'pineapple', 'Grapes' ], 'Fruits' => [ 'Apple', 'Orange', 'Banana', 'Grapes' ], 'difference' => [ 'pineapple', 'Apples' ] };

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1186948]
Approved by Corion
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-05-26 09:15 GMT
Find Nodes?
    Voting Booth?

    No recent polls found