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

Hiya Perl Monks!,
I'm a Perl newbie and i'm a little confused about reading arrays. I would like to search an array for something that does not exist in it. If it doesn't, then print a message.
So if i have "abc" and i search array ("bcd","efg","hij","klm") i want to print a message. If "abc" appears in the array nothing should happen.
The problem is, if i use 'ne' then it prints the message many times! Help!

Many thanks, Abi

Replies are listed 'Best First'.
Re: does not exist in an array
by eieio (Pilgrim) on Mar 10, 2005 at 02:53 UTC
    grep works well in this situation:
    my @values = qw( bcd efg hij klm ); print "not found" unless grep { 'abc' eq $_ } @values;
      Also remember that grep will read the entire array into memory.....

      Frequently creates problem when you Tie::File a large file to an array, Tie::File won't read it, but grep(if present) will....

      Manav
      thanks, thats great! :)
Re: does not exist in an array
by mkirank (Chaplain) on Mar 10, 2005 at 06:57 UTC
    if you want to search for more words u can use this

    #!perl -slw my @values = qw( bcd efg hij klm ); my @values2 = qw ( abc bcd ); my %to_search; @to_search{@values} = (); my @found = grep { exists $to_search{$_} } @values2; print @found;
Re: does not exist in an array
by dmorelli (Scribe) on Mar 10, 2005 at 02:40 UTC
    What message is it printing? Show us the code you've tried so far so we can see what's happening in there.
      er its just a message, like "does not exist in this array". My code pretty much consists of:
      foreach (@array) { if ($text ne $_) { print "doesn't exist!"; } }
      But this doesn't work as it prints the message out several times. I just want it to print the message once if the text i enter doesn't match any of the array elements.

      thanks for your help!

        Ok. Here's one way that stores the found status of the search and terminates the foreach loop early if/when it does find one.

        #! /usr/bin/perl -w use strict; # What we're trying to find my $target = shift; # Flag to hold found status my $found; # List to search through my @words = qw/bcd efg hij klm/; # Look through the list foreach (@words) { # This will be true if they match $found = $target eq $_; # We found one, get out of this loop early last if $found; } # Print if nothing was found print "$target not found$/" unless $found;

        But you just can't beat eieio's use of grep() below. Fantastic, ++

Re: does not exist in an array
by trammell (Priest) on Mar 10, 2005 at 15:13 UTC
    You should consider using a hash for storage instead of an array, if the hash is primarily for lookups. Or convert your array into a hash for the lookup:
    my @array = qw/ bcd efg hij klm /; my %hash = map { $_, 1 } @array; my $query = 'abc'; if (exists $hash{ $query }) { print "'$query' is in the hash...\n"; }
Re: does not exist in an array
by DrHyde (Prior) on Mar 10, 2005 at 09:25 UTC
    Mmmm, tasty tasty homework.