Hello mbgbioinfo,

It’s a pity you haven’t followed Marto’s advice to use strict and to properly indent your code. You are making it much harder on yourself by not doing so. :-(

To use SEEK_SET, you need to add use Fcntl ':seek'; at the top of your script. See seek and Fcntl. Now to the main issue:

foreach (@words) { print $fh "$_\n"; chomp; }

There is no point in adding chomp here, it accomplishes nothing. It is the variable $choice which has a terminal newline that needs to be removed:

my $choice = $line; chomp $choice;

I notice also that the following logic is wrong:

for ($i=0; $i<@letters; $i++) { if ($letters[$i] eq $guess) { $blankword[$i]=$guess; $right=1; } }

$letters[$i] is a single character, but $guess is a string of characters. You need to compare each letter of $choice with the corresponding letter in $guess. (And when you re-write that loop, be careful to ensure that $i never exceeds the index of the last letter in the shorter of the two strings.)

P.S. Whenever you update a post (in this case, your OP), please leave the original intact and clearly mark additions as updates, so that monks coming to this thread in the future will be able to make sense of the replies.

Hope that helps,

Update: Re-wrote the P.S. to make it clear that when updates are added, the original should not be removed.

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^3: hangman game by Athanasius
in thread hangman game by mbgbioinfo

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.