Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: hangman game

by Athanasius (Archbishop)
on May 28, 2015 at 15:15 UTC ( [id://1128149]=note: print w/replies, xml ) Need Help??


in reply to Re^2: hangman game
in thread hangman game

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,

Replies are listed 'Best First'.
Re^4: hangman game
by mbgbioinfo (Novice) on Jun 06, 2015 at 19:12 UTC
    It did help indeed. Thanks again. Cheers :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1128149]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-29 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found