in reply to Sub array in Array of array

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.™)

Replies are listed 'Best First'.
Re^2: Sub array in Array of array
by Gulliver (Monk) on May 19, 2011 at 14:59 UTC

    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;