Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

compare two reference in perlway

by k_manimuthu (Monk)
on Oct 19, 2015 at 09:52 UTC ( [id://1145314]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I have two reference A and B. Ref B is the part of ref A. I am trying to iterate A and check ref B is the part of ref A. Below I place the code which i tried. I am expecting your suggestion to compare the two reference in perl way.

Thanks

use Data::Dumper; my $part = { 'c' => {}, 'func' => [] }; my $main = { 'a' => { 'b' => { 'c' => {}, ### equals to $part hash reference 'func' => [] }, 'func' => [ 'd', 'e' ] } }; walk ($main); sub walk(){ my ($item, $path) = @_; if (ref $item eq 'ARRAY') { foreach (@$item) { walk($_, $path); } ### Need to change the condition/logic here... if ($item eq $main) { print "\nExpected data match here ... ", Dumper $part; } } elsif (ref $item eq 'HASH') { foreach (sort keys %$item) { push @$path, $_; walk($item->{$_}, $path); pop @$path; } } }

Replies are listed 'Best First'.
Re: compare two reference in perlway
by Eily (Monsignor) on Oct 19, 2015 at 12:14 UTC

    At first I thought about Test::More's is_deeply, which compares two nested data structures for testing purposes. But it seems like Data::Compare does the same job pretty well for common programs. You can either use one of the two, or look at the source code to understand how they work.

Re: compare two reference in perlway
by mr_ron (Chaplain) on Oct 19, 2015 at 14:15 UTC

    Besides Data::Compare proposed by Eily, there are also Data::Walk and Data::Walker. When you put it together the problem seems to have a reasonably simple and neat solution:

    use strict; use warnings; use Data::Walk; use Data::Compare; sub find_structure_part { my $search_space = shift; my $target = shift; my $found = 0; walk sub { if (not $found and Compare($_, $target)) { $found = 1 } }, $search_space; return $found; }
    Ron
Re: compare two reference in perlway
by stevieb (Canon) on Oct 19, 2015 at 14:09 UTC

    I'm with Eily. Data::Compare does this work very well. Here's an example:

    use warnings; use strict; use Data::Compare; my $part = { 'c' => {}, 'func' => [] }; my $main = { 'a' => { 'b' => { 'c' => {}, 'func' => [] }, 'func' => [ 'd', 'e' ] } }; for my $inner (keys %$main){ for my $key (keys %{ $main->{$inner} }){ my $its_here = Compare $part, $main->{$inner}{$key}; if ($its_here){ print "found at location $key\n"; } } }
Re: compare two references in the Perl way
by Athanasius (Archbishop) on Oct 19, 2015 at 14:16 UTC

    Hello k_manimuthu,

    Combining your recursive approach with the Test::Deep::eq_deeply function suggested by Eily’s post, I came up with the following:

    #! perl use strict; use warnings; use Test::Deep::NoTest; my $x = { c => {}, func => [], }; my $x1 = { func => [], c => {}, }; my $x2 = { func => [], c => { s => 't' }, }; my $main = { a => { b => { c => {}, ### equals to $part hash reference func => [], }, func => [ 'd', 'e', ], }, }; printf "%s found\n", walk($main, $x ) ? 'Match' : 'No match'; printf "%s found\n", walk($x, $x1) ? 'Match' : 'No match'; printf "%s found\n", walk($main, $x2) ? 'Match' : 'No match'; sub walk { my ($whole, $part) = @_; my $ref = ref $whole; return 1 if eq_deeply($whole, $part); if ($ref eq 'ARRAY') { for (@$whole) { if (eq_deeply($_, $part)) { return 1; } else { return 1 if walk($_, $part); } } } elsif ($ref eq 'HASH') { for (keys %$whole) { if (eq_deeply($whole->{$_}, $part)) { return 1; } else { return 1 if walk($whole->{$_}, $part); } } } return 0; }

    Output:

    0:08 >perl 1414_SoPW.pl Match found Match found No match found 0:08 >

    The results shown are as expected; however... (disclaimer) a lot more, and more rigorous, testing is still required!

    Anyway, hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks a lot for all your suggestions...

Re: compare two reference in perlway
by GotToBTru (Prior) on Oct 19, 2015 at 13:02 UTC

    Comparing the value of one ref to another would only work if they were created to point to the same variable. Perl doesn't compare the contents of references automatically, and a simple eq won't work for anything other than a scalar.

    DB<1> $ans1=42 DB<2> $ref1=\$ans1 DB<3> $ref2=\$ans1 DB<4> if ($ref1 eq $ref2) { print "match\n" } match DB<5> $ans2 = 42 DB<6> $ref2 = \$ans2 DB<7> if ($ref1 eq $ref2) { print "match\n" } DB<8> if ($$ref1 eq $$ref2) { print "match\n" } match
    Dum Spiro Spero

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1145314]
Approved by marto
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found