Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Get hangman words from text file

by rjt (Deacon)
on Dec 04, 2012 at 22:34 UTC ( #1007166=note: print w/ replies, xml ) Need Help??


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.)


Comment on Re: Get hangman words from text file
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-08-22 12:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (156 votes), past polls