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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Sub array in Array of arrau
by LanX (Saint) on May 19, 2011 at 11:46 UTC
    the answer you got for Eliminate Array of array duplication is also suitable for checking if the subarray is already included.

    you should try to understand the code you are getting here!

    Cheers Rolf

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Sub array in Array of array
by locked_user sundialsvc4 (Abbot) on May 19, 2011 at 14:06 UTC

    It might help for me to point out that your “array of arrays,” in the original post, is perceived by Perl as being one array with (currently...) two elements in it, as follows:

    1. an arrayref that refers to the anonymous array:   12,13,14,15,16,17,18,19
    2. an arrayref that refers to the anonymous array:   20,21,22,23

    Strictly speaking, either or both of these anonymous arrays could have other references that point to them ... see perldoc perlref.

    So, if you want to find out if this array already contains a particular sub-array (that is to say, a particular arrayref, you will have to write (or find in CPAN ...) a sub that, given two arrayrefs, will determine if the two are equal.   Your logic then becomes something like ... (where @master_array is the array in your original post, and $new_array is an arrayref that you might wish to add to it, and arrays_are_equal() is something that you find or write) ...

    my $was_found = 0; foreach my $member (@master_array) { if (arrays_are_equal($new_array, $member)) { $was_found = 1; last; # no need to keep looking ... } }; push @master_array, $new_array unless ($was_found);

    (Caution... extemporaneous coding... it might not even compile... your mileage may vary.™)

      List::Compare can check for subsets and can check if two arrays are equivalent.

      # is_LsubsetR() #Return a true value if the first argument passed to the #constructor ('L' for 'left') is a subset of the second #argument passed to the constructor ('R' for 'right'). $LR = $lc->is_LsubsetR; #Return a true value if R is a subset of L. $RL = $lc->is_RsubsetL;
Re: Sub array in Array of array
by John M. Dlugosz (Monsignor) on May 19, 2011 at 13:48 UTC
    There is no simple primitive for that. You'll have to check each existing element and see if it's the same as the proposed addition.

Re: Sub array in Array of array
by wind (Priest) on May 19, 2011 at 17:16 UTC
    use strict; use warnings; my @AoA = ( [12,13,14,15,16,17,18,19], [20,21,22,23], ); my @a = (14,15,16,17,18,19); my @index = grep { my $i = $_; my %seen = map {$_ => 1} @{$AoA[$i]}; ! grep {!$seen{$_}} @a; } keys @AoA; print "\@a is within index $_\n" for @index;
Re: Sub array in Array of arrau
by Anonymous Monk on May 19, 2011 at 11:32 UTC
    how to i will check this?

    By editing some code from you copy/paste the FAQ?