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:
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 |