in reply to macthing a word a in array of words
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.#!/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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: macthing a word a in array of words
by zarlag (Initiate) on Apr 03, 2011 at 13:57 UTC | |
by Marshall (Canon) on Apr 03, 2011 at 23:22 UTC |