Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How to read from a file instead from a hard coding list.

by 300zxmuro (Initiate)
on Apr 25, 2012 at 03:19 UTC ( #966975=perlquestion: print w/ replies, xml ) Need Help??
300zxmuro has asked for the wisdom of the Perl Monks concerning the following question:

Hello almighty Monks! Hope all is well with you and your brain because mine is burning with a simple question. I have this hangman game. i need to know what change I should make to read from a file I have in the system. Let us say I have a file with a million words, I want to pick from that file the words, so again to pick a word from a particular file that I have in my server/PC. ALSO I would like to tell the player how many attempts it took to guess the word. Thanks a lot! Here is the code:

#!/usr/bin/perl -w # hangman.pl # use strict; use warnings; my @words=qw( portion answers printer program ); my @guesses=(); my $wrong=0; my $choice=$words[rand @words]; my $hangman="0-|--<"; my @letters=split(//, $choice); my @hangman=split(//, $hangman); my @blankword=(0) x scalar(@hangman); while ($wrong < scalar(@hangman)) { foreach my $i (0..$#letters) { if ($blankword[$i]) { print $blankword[$i]; } else { print "-"; } } print "\n"; if ($wrong) { print @hangman[0..$wrong-1]; } print "\n Your Guess: "; my $guess=<STDIN>; chomp $guess; my $right=0; for (my $i=0; $i<@letters; $i++) { if ($letters[$i] eq $guess) { $blankword[$i]=$guess; $right=1; } } $wrong++ unless($right); if (join('', @blankword) eq $choice) { print "The word is '$choice'... You got it right!\n"; exit; } } print "$hangman\nBetter luck next time, the word was '$choice'.\n";

Comment on How to read from a file instead from a hard coding list.
Download Code
Re: How to read from a file instead from a hard coding list.
by stevieb (Hermit) on Apr 25, 2012 at 03:24 UTC

    What is the problem exactly? What do you want to get from a file, and where do you need what you want from the file?

      I need the words to come out from a file that I have at certain location. The files have all words to be used at the game. Thanks a bunch in advance!

Re: How to read from a file instead from a hard coding list.
by lidden (Deacon) on Apr 25, 2012 at 04:10 UTC
    If I undestand correctly you should change:

    my @words=qw( portion answers printer program );

    To:

    my $file = '/path/to/file'; my @words; open my $fh, '<', $file or die "Could not open $file: $!"; while(<$fh>){ push @words, split; } close $fh or die $!;

    Untested.

      Thank you ALL, I really appreciate your time and ideas. It is possible to help me with the piece that counts the number of attempts the gamer used and posted at then end? THANKS AGAIN!!!

Re: How to read from a file instead from a hard coding list.
by 2teez (Priest) on Apr 25, 2012 at 05:46 UTC

    You could slurp the whole file into a variable like this

    use warnings; use strict; my $filename//=$ARGV[0]; # i.e script.pl path_to_file my $data; open my $fh,'<',$filename or die"can't open file:$!"; $data=do{local $/; <$fh>}; ## slurp the whole file once close $fh; my @guesses=(); my $wrong=0; my @words=split/\s+?/,$data; my $choice=$words[rand @words]; ....... # rest of the program

    hope this helps

Re: How to read from a file instead from a hard coding list.
by JavaFan (Canon) on Apr 25, 2012 at 08:36 UTC
    Replace
    my @words=qw( portion answers printer program );
    with
    chomp(my @words = `cat /path/to/file`);
    and you should be done.
      my @words = `cat /path/to/file`

      Are you sure the OP uses some kind of unix? Windows has no cat command out of the box. And by the way: Why spawn a new process to read a file when perl can read the file without assistance? File::Slurp does a nice job.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Windows has no cat command out of the box.
        Does Windows has perl out of the box?
        Why spawn a new process to read a file when perl can read the file without assistance?
        Who cares about a new process? It isn't that we're doing this a billion times in a loop, and considering we're going to read a couple of Mb from disk, I don't think creating a new process is going to form a bottleneck.
        File::Slurp does a nice job.
        And that one comes with Windows out of a box? Really, you need a module to read in a file? What's wrong with
        chomp(my @words = do {local(@ARGV, $/) = "/path/to/file"; <>});
        or some other equivalent lines if you're willing to spend more keystrokes than `cat` is taking? Why is that using a module to do this is ok, but using a program isn't?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2014-07-25 09:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (170 votes), past polls