#!C:\Perl64\bin init_words(); print "What is your name Mr. \n"; $name = <STDIN>; chomp ($name); if ($name =~ /^randal\b/i){ print "Hello, Randal, How are you doing \n"; } else{ print "Hello, $name!\n"; print "Tell the secret word\n"; $guess = <STDIN>; chomp ($guess); while (!good_word ($name,$guess)){ print "Wrong, please try again\n"; $guess = <STDIN>; chomp ($guess); } } sub init_words { open (WORDSLIST, "wordslist.txt") || die "can't open wordslist: $!"; $k = 1; $a = 0; $b = 0; while (defined ($name = <WORDSLIST>)) { if ($k % 2 == 0){ chomp ($name); @words1[$a] = $name; ++$k; ++$a; } else{ chomp ($name); @words2[$b] = $name; ++$k; ++$b; } } close (WORDSLIST) || die "couldn't close wordlist: $!"; } sub good_word { my ($somename, $someguess) = @_; $somename =~ s/\W.*//; $somename =~ tr/A-Z/a-z/; if ($somename eq "randal"){ return 1;} else { #$n = 0; $t = scalar @words1; $u = scalar @words2; print "the words2 array is @words2"; for ($d = 0; $d < $u; $d++) { print "currently name in array is @words2[$d]\n"; print "The value of somename is $somename \n"; if ("@words2[$d]" eq "$somename"){ print "test"; return 1;} } #print "The final value of d is $d"; #print " The final value of array is @words1[$d]"; #if ("groucho" eq $someguess){ #return 1;} #else{ #while ($n < $t){ #if (@words1[$n] eq $someguess) { #return 1;} #else { ++$n}; } } # } #}
the main purpose of the program is that if i input a name along with a guess, perl program should look for all names stored in @words2 and if it find a match, 1 should be returned.
I have wordlist of 12 words where every odd number of list items will be put into wordslist2. For some reason, this code is only working if last words of the wordslist are input.
Any suggestions??In reply to program doesnt work for all elements of the array by iThunder
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |