By "member array ref" of an AoA you can understand two things:

  1. The "member array ref" points to the same memory object as the element in the AoA points to.
  2. The "member array ref" points to a different memory object which has an identical "printable representation", or other criterion of comparison you may see fit.

From your post, I assume you want (2). Here's an implementation of both (1) an (2) together with a test. Be aware that my implementation of (2) is simple - it relies on stringification by joining with $;, which does not work if the arrays in the AoA are themselves deep data structures and may give incorrect results if you use the value of $; in your subarray values.

#!/usr/bin/perl use strict; use warnings; sub member_as_object { my $aref = shift; my $AoAref = shift; return grep {$aref eq $_} @{$AoAref}; } sub stringify { return join $;, @{(shift)}; } sub member_as_stringified_value { my $astring = stringify(shift); my $AoAref = shift; return grep {$astring eq stringify($_)} @{$AoAref}; } # building a test case below my @AoA = ( [ 'beethoven', 'berlioz', 'strauss' ], [ 'wagner', 'mozart', 'brahms' ], [ 'sibelius', 'elgar', 'dvorak' ] ); my %arrays = ( member_obj => $AoA[1], member_copy => [ @{$AoA[1]} ], non_member => [ qw/foo bar baz/ ] ); my %compare_subs = ( member_as_object => \&member_as_object, member_as_stringified_value => \&member_as_stringified_value ); for my $csub (sort keys %compare_subs) { for my $array (sort keys %arrays) { my $result = $compare_subs{$csub}->($arrays{$array}, \@AoA) ? 'true' : 'false +'; print "$csub($array) = $result\n"; } }

In reply to Re: Checking if an Array is a Member of an AoA by calin
in thread Checking if an Array is a Member of an AoA by neversaint

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.