in reply to two dimensional array lookup

The most straightforward way to do what you want is to use a hash to keep track of the elements that already exist, then scan the hash to determine duplicates. Something like:
#!/usr/bin/perl -w use strict; use vars qw(@all @dup); @all=(["what", "is", "the", "matrix"],["matrix", "reloaded", "is", "no +", "good"]); my(%h1,%h2); # for the two halves of your array grep { $h1{$_}=1 } @{$all[0]}; grep { $h2{$_}=1 } @{$all[1]}; @dup = ( [ map { $h2{$_} ? 1 : 0 } @{$all[0]} ], [ map { $h1{$_} ? 1 : 0 } @{$all[1]} ]); print '([', join(',',@{$dup[0]}), '], [', join(',',@{$dup[1]}), "])\n";
Oh, and map executes a block of code for each element in the array, returning an array consisting of the return value from each block of code.

Replies are listed 'Best First'.
Re: Re: two dimensional array lookup
by Skeeve (Parson) on Jul 17, 2003 at 06:31 UTC
    I would write this
    grep { $h1{$_}=1 } @{$all[0]};
    like this
    grep { $h1{$_}=1; undef; } @{$all[0]};
    in order not to get all my elements from @{$all[0]} as a return value of grep.

    I even might write:

    @h1{@{$all[0]}}=(1) x @{$all[0]};
      Why use grep at all if you don't need a return value?
      $h1{$_}=1 for @{$all[0]};