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

Hi im Czar, mainly named ZarLag. but for get my intro save that for a later time ok here is my question. i have 2 scalars and 1 array
my $word; my @wrds; my $w; my $tries=0; while($tries!=2){ print "enter a word:"; $word=<STDIN>; chop $word; while($word != @wrds[$w]){ $w++; if($word = @wrds[$w]){print "found MACTH!!";} } $tries++; }
ok so i want to ask for a input and after the user inputs a word the program than goes threw every index in the array of words. if the word is not in the array than print word is not in array. if it is in array than print it is in array. im working on a program that requires this algorithm/protocol but i seem to have a problem matching a input with a initialized value in a array. improvement in code or help with this problem of my would be nice i been struggling with it for about a week and cant seem to find how to make it work. how would i macth a string that is in array. thanks for the help in advanced.

Replies are listed 'Best First'.
Re: macthing a word a in array of words
by Marshall (Canon) on Apr 02, 2011 at 15:54 UTC
    Simple program that builds the array and checks for dupes.

    #!/usr/bin/perl -w use strict; my @words; my $tries=0; while($tries!=2){ print "enter a new word:"; my $word=<STDIN>; chomp $word; if (grep{/$word/}@words) { print "$word already exists!\n"; $tries++; } else { push @words, $word; $tries=0; } } print "2 tries to enter a new word are up!\n"; print "Unique Words were:@words\n";
    Using indicies is usually not the best way in Perl to search an entire array - there are better iterators. Of course many improvements could be made to the above code. List::More::Util has a first() routine that will be faster than my grep, but that is a speed thing not an algorithm thing.
      nice almost what i was looking for but thier seems to be a bug or it just how grep compares the words. $word doesnt exist on a letter or a of a $word exist. if you type in:noo than type in: oo it will say 'oo' exist. thanks for the replie. i try many more things and see what i can do. Thank you for the help YALL.
        yes, the regex could be better. When dealing with user input, you should allow whitespace before and after the input. Some common idioms:
        s/^\s+//; #remove leading whitespace s/\s+$//; #remove trailing whitespace
        Now that whitespace is not a factor, for the match then anchor the search term to front and rear. /^dog$/; matches dog but not doggie let me know how you get along with those hints.
Re: macthing a word a in array of words
by wind (Priest) on Apr 02, 2011 at 20:20 UTC

    Take a look at perlop for the list of operators. You need to use eq and ne for string comparison instead == and !=. Also, grep is a good function to be familiar when dealing with arrays.

    use strict; my @words = qw(foo bar baz biz); for (1..2) { print "enter a word:"; chomp(my $word = <STDIN>); if (grep {$word eq $_} @words) { print "found MATCH!!"; last; } }
Re: matching a word a in array of words
by Anonymous Monk on Apr 02, 2011 at 14:42 UTC

    Where|How do you populate @wrds ("words" is not too long to type, mind you) before entering the while loop?

    In if($word = @wrds[$w]), as $w is never being incremented & remains undefifned, $word is assigned undef every time. Use eq operator to test equality of strings (== to test equality of numbers).

    To match a user given word in a pre-populated array, iterate over the array of words inside the trial loop.

      In if($word = @wrds$w), as $w is never being incremented & remains undefined, $word is assigned undef every time.

      Ah, never mind that as I missed $w++ earlier. (Found other problems in code but too tedious to point them out.)

      OP, what is your pseudocode?