in reply to Re: does not exist in an array
in thread does not exist in an array

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!

Replies are listed 'Best First'.
Re^3: does not exist in an array
by dmorelli (Scribe) on Mar 10, 2005 at 03:03 UTC
    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, ++