in reply to does not exist in an array

What message is it printing? Show us the code you've tried so far so we can see what's happening in there.

Replies are listed 'Best First'.
Re^2: does not exist in an array
by Anonymous Monk on Mar 10, 2005 at 02:57 UTC
    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, ++