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

Hello, I am having an array of array consisting of three elements
@arr1=(1,2,3,4,5); @arr2=(6,7,8,9,10); @arr3=(11,12,13,14,15);
@mainarr contains all the three arrrays. How can i check whether an element is present in the mainarr? For single arrays we can use like
grep(/tofind/i,@arr)
.... can anyone suggest something similar to that??????

Replies are listed 'Best First'.
Re: How to check whether an element is present in an array of array
by kilinrax (Deacon) on Jun 05, 2009 at 10:59 UTC
    Well, if you have something like:
    my @arr1 = (1,2,3,4,5); my @arr2 = (6,7,8,9,10); my @arr3 = (11,12,13,14,15); my @mainarr = (\@arr1, \@arr2, \@arr3);
    Or more simply:
    my @mainarr = ([1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15]);
    Then you can check for the existence of an element in each subarray just by deferencing with map:
    grep { $_ == $tofind } map { @$_ } @mainarray;
    (note the use of '==' instead of a regex to match numbers)

    However, that isn't terribly efficient, depending on the nature of the data you're actually searching. Some more context would be helpful here.

Re: How to check whether an element is present in an array of array
by dpetrov (Acolyte) on Jun 05, 2009 at 11:24 UTC
    May be I am wrong, but... if @mainarr is array of array...
    @arr1=(1,2,3,4,5); @arr2=(6,7,8,9,10); @arr3=(11,12,13,14,15); my @mainarr = ( \@arr1, \@arr2, \@arr3 );

    ... you probably could do:

    my %index = map { map { $_ => 1 } @$_ } @mainarr; print "found" if $index{ 'tofind' };
Re: How to check whether an element is present in an array of array
by irah (Pilgrim) on Jun 05, 2009 at 10:44 UTC

    The direct answer available in perldoc.

    @mainarr = (1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12, 13, 14, 15); @is_tiny_prime = (); for (@primes) { $is_tiny_prime[$_] = 1 } # or @istiny_prime[@primes] = (1) x @primes;

    Now you check whether $is_tiny_prime[$some_number].

    If the number available in array, it print 1, else 0.

Re: How to check whether an element is present in an array of array
by bichonfrise74 (Vicar) on Jun 05, 2009 at 15:44 UTC
    It looks like your post is very similar to this node Array of array .
    Have you check this yet?
Re: How to check whether an element is present in an array of array
by Anonymous Monk on Jun 05, 2009 at 10:40 UTC
    You do not have an array of arrays ree.