http://www.perlmonks.org?node_id=1007166


in reply to Get hangman words from text file

If you put your words on separate lines in the text file (as is usually the case in free dictionaries you can download off the Internet), you can do something like:

open my $file, '<', 'words.txt' or die "Can't open words: $!"; my @listOfWords = (<$file>); chomp @listOfWords; close $file;

As for your other error, if you used the exact code in your second block, you actually have round braces instead of the square subscript braces ($listOfWords($count) should be $listOfWords[$count]), and you are missing a closing brace: } at the end of the block. Either of those could cause other errors which might not be obvious if the @hangman error was the first one you saw. In addition to that:

$Word = split ' ', $line;

... will return the count of words found in $line, since $Word is a scalar variable which puts split into scalar context. You would instead write something like:

push @listOfWords, split(' ', $line);

(This gets rid of the need for the $count variable.)