in reply to hangman game

Hello mbgbioinfo,

There is another problem with the code shown — in this snippet:

open(MYFILE, "<words") or die "Opening words: $!"; print MYFILE @words; close(MYFILE);

the file “words” is opened for reading, but you attempt to write to it.

BTW, there is no need to close the filehandle and then re-open it. Open it once, for reading and writing, write to it, then reposition the filehandle to the beginning of the file before reading:

my $filename = 'words'; open(my $myfile, '+>', $filename) or die "Cannot open file '$filename' for writing and reading: $!"; print $myfile @words; seek($myfile, 0, SEEK_SET); srand; rand($.) < 1 and $line = $_ while <$myfile>; close $myfile or die "Cannot close file '$filename': $!";

Notes:

Hope that helps,

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

Replies are listed 'Best First'.
Re^2: hangman game
by mbgbioinfo (Novice) on May 28, 2015 at 10:36 UTC
    Dear Athanasius, Thank you for your time. When I put in my code your solution it writes this "Argument "SEEK_SET" isn't numeric in seek at testh line 13.". Furthermore, it is using all the words of the list @words as one. Instead of what you said, I put this, but I cannot remove the /n character. In the output, is is always asking for one extra character in the end of each word.
    my @words = qw( internet answers printer program ); open my $fh, '>', "words.txt" or die "Cannot open words.txt: $!"; foreach (@words) { print $fh "$_\n"; chomp; } close $fh; srand; open MYFILE, "<words.txt" or die "Could not open words.txt: $!\n"; rand($.)<1 and ($line=$_) while <MYFILE>; close MYFILE;

      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,

        It did help indeed. Thanks again. Cheers :)