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 | |
by Athanasius (Archbishop) on May 28, 2015 at 15:15 UTC | |
by mbgbioinfo (Novice) on Jun 06, 2015 at 19:12 UTC |