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,


In reply to Re: compare two references in the Perl way by Athanasius
in thread compare two reference in perlway by k_manimuthu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.