Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Assigning a file to an Array.

by elle45purple (Initiate)
on Feb 12, 2009 at 15:08 UTC ( [id://743335]=perlquestion: print w/replies, xml ) Need Help??

elle45purple has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks! I'm new to perl and wanted to ask the people here if they could help me fix a simple hangman program that i cant get to work. I'm trying to get this program to open the file "dictionary.txt" Then i want to assign that file to @words. However, immediately there's a problem with that, two question marks pop up at the beginning which were not in the original file (I've tried using the chomp function). I then want to choose a word at random from that file and split it so that each letter is a different element in @letters so that i can turn the number of elements into a single number. The array @blankword is used to mark which letter the player has guessed successfully. (0) x scalar(@letters) creates a list that is as long as the number of elements in @letters, which is stored in @blankword. As letters are guessed, these 0s are changed to letters which will mark the positions of the correctly guessed letters.But the number of 0s are always too many. This is the code for my program.
#!/usr/bin/perl -w open (DICT, "/Users/programming/dictionary.txt"); @words=<DICT>; @guesses=(); $wrong=0; $choice=$words[rand @words]; $hangman=" O\n,/,|,\\,\n/, \\"; @letters=split(//, $choice); @hangman=split(/,/, $hangman); @blankword=(0) x scalar(@letters); Outer: while ($wrong<@hangman) { foreach $i (0..$#letters) { if ($blankword[$i]) { print $blankword[$i]; } else { print "-"; } } print "\n"; if ($wrong) { print @hangman [0..$wrong-1] } print "\n Your Guess: "; $guess=<STDIN>; chomp $guess; foreach(@guesses) { next OUTER if ($_ eq $guess);p } $guesses[@guesses]=$guess; $right=0; for ($i=0; $i<@letters; $i++) { if ($letters[$i] eq $guess) { $blankword[$i]=$guess; $right=1; } } $wrong++ if (not $right); if (join('', @blankword) eq $choice) { print "you got it right!\n"; print "the word was $choice!\n"; exit; } } @hangman=join('', @hangman); print "@hangman\nSorry, the word was $choice. \n";

The following code works ok but i cant seem to change it to get it to choose the words from the dictionary file.

#!/usr/bin/perl -w @words=qw(printer internet); @guesses=(); $wrong=0; $choice=$words[rand @words]; $hangman=" O\n,/,|,\\,\n/, \\"; @letters=split(//, $choice); @hangman=split(/,/, $hangman); @blankword=(0) x scalar(@letters); Outer: while ($wrong<@hangman) { foreach $i (0..$#letters) { if ($blankword[$i]) { print $blankword[$i]; } else { print "-"; } } print "\n"; if ($wrong) { print @hangman [0..$wrong-1] } print "\n Your Guess: "; $guess=<STDIN>; chomp $guess; foreach(@guesses) { next OUTER if ($_ eq $guess);p } $guesses[@guesses]=$guess; $right=0; for ($i=0; $i<@letters; $i++) { if ($letters[$i] eq $guess) { $blankword[$i]=$guess; $right=1; } } $wrong++ if (not $right); if (join('', @blankword) eq $choice) { print "you got it right!\n"; print "the word was $choice!\n"; exit; } } @hangman=join('', @hangman); print "@hangman\nSorry, the word was $choice. \n";


and here is the contents of the dictionary file which i think should be readable from perls perspective.
Printer
Internet
Mouse
Keyboard
Monitor

Thank you for reading this and i would appreciate it very much if you could help me.
elle45purple

Replies are listed 'Best First'.
Re: Assigning a file to an Array.
by kyle (Abbot) on Feb 12, 2009 at 16:02 UTC

    As JavaFan has already pointed out, you don't check to see whether your open succeeded. The usual way to do this is the "do or die" idiom.

    my $dict_file = '/usr/share/dict/words'; open my $dict_fh, '<', $dict_file or die "Can't read '$dict_file': $!";

    Be sure to include $! in the error message when you die so you know why it didn't work.

    I agree also with ikegami that you should Use strict and warnings.

    The reason your @blankword winds up too long is that each of @words has a newline at the end of it. You can take care of that with chomp similar to how Ciclamino suggests or just do what you're doing now and then take care of @words before you select one.

    open my $dict_fh ... my @words = <$dict_fh>; chomp @words;

    I have not read the bulk of your code, so there could be more errors there as well.

    As an aside, since you only ever select one word from your dictionary, you don't have to store the whole array of them. See How do I select a random line from a file?

Re: Assigning a file to an Array.
by olus (Curate) on Feb 12, 2009 at 15:43 UTC

    Write another program that just reads the dictionary file and check if it is ok, or create a new one with the words you want. My guess is that there is something wrong with the file because your program worked just fine with me.

    $ perl hangman.pl -------- Your Guess: I -------- O Your Guess: P P------- O Your Guess: a P------- O / Your Guess: s P------- O /| Your Guess: d P------- O /|\ Your Guess: f P------- O /|\ / Your Guess: g O /|\ / \ Sorry, the word was Printer .

    update: You should notice that the label Outer is not case insensitive.

Re: Assigning a file to an Array.
by ikegami (Patriarch) on Feb 12, 2009 at 15:15 UTC
    Use
    <code>..</code>
    not
    <code>..<code>

    And please please please use use strict; use warnings; in your programs!

Re: Assigning a file to an Array.
by JavaFan (Canon) on Feb 12, 2009 at 15:20 UTC
    You didn't check the result of open. I've no idea whether that's related to your problem, but soon after the open, your code becomes unreadable.
Re: Assigning a file to an Array.
by Zen (Deacon) on Feb 12, 2009 at 15:28 UTC
    Use hashes for your dictionary.
Re: Assigning a file to an Array.
by Ciclamino (Sexton) on Feb 12, 2009 at 15:47 UTC
    Instead of @words = <DICT>; do:
    while (<DICT>) { chomp; push @words, $_; }
      Hmmm, why so ?

      I prefer the chomp(@words = <DICT>); idiom myself.

      A user level that continues to overstate my experience :-))
      Hello Ciclamino

      Thanks alot for replying I tried using your code and it came out with this->
      ----------- Your Guess: i ----------- O Your Guess: o ---o------- O Your Guess: m -m-o------- O Your Guess: u -m-o-u----- O Your Guess: s -m-o-u-s--- O Your Guess: e -m-o-u-s-e- O Your Guess: p -m-o-u-s-e- O / Your Guess: y -m-o-u-s-e- O /| Your Guess: w -m-o-u-s-e- O /|\ Your Guess: n -m-o-u-s-e- O /|\ / Your Guess: b O /|\ / \ Sorry, the word was mouse.

      When i make a list directly in the program this does not happen.
      elle45purple

Log In?
Username:
Password:

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

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

    No recent polls found