P.S.  Using hashes will eliminate duplicate entries from both lists.  My 
assumption in suggesting this was that that wasn't a problem, but it is 
something that should be made explicit.

Cheers,
Jeff

At 03:24 PM 6/8/01 -0700, Jeff Yoak wrote:

>Code to do what you are looking for is inserted into the loop below, with 
>comments at the end.
>
>At 10:02 PM 6/8/01 +0000, scott lutz wrote:
>>I have a question that to me seems like it would be part of the 
>>foundation of Perl.
>>
>>@possible_matches = qw( list of items to match against  );
>>
>>
>>@list = qw(     imagine that there is an unknown
>>                 number of elements in here      );
>>
>>foreach $list (@list) {
>>         # now match againt each value in @possible_matches, one-by-one,
>
>         foreach my $item (@possible_matches) {
>                 if($list eq $item){
>                         # Do whatever you like, here, print it, store it, 
> etc.
>                 }
>         }
>
>>}
>
>As far as it being in part of the "fundamentals," the best thing I can 
>suggest is that you consider using hashes.  When you want to know if 
>something is "in" a list, hashes leap naturally to mind.
>
>First you make the elements of your arrays above into keys of a hash.  The 
>values don't matter.  If you don't also need them in arrays for some other 
>purpose, you might do this directly with something like:
>
>my %possible_matches;
>@possible_matches{qw(   list of items to match against  )}=();
># This actually efficiently assigns to the hash with something called slicing.
># You'll find it in the standard books, including the llama on about page 73.
># (I don't have it here now.)
># The values, which you don't care about, will all be undef.
>my %list;
>@list{qw(item whatever baz bar)} = ();
>
>Now to get the same thing as above, you can ask more directly like this:
>
>foreach my $item (keys %possible_matches){
>         if(exists $list{$item}){
>                 # do whatever
>         }
>}
>
>or more succinctly:
>
>my @matches = grep {exists $list{$_}} keys %possible_matches;
>
>Enjoy!
>
>Cheers,
>Jeff
>


Reply via email to