Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Get hangman words from text file

by rjt (Curate)
on Dec 04, 2012 at 22:34 UTC ( [id://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.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-16 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found