#!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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.