Advertisement

11.26.2007 at 05:49AM PST, ID: 22982316
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

5.8

Saving to a file - hangman

Asked by catonthecouchproductions in Perl Programming Language

I have a hangman script that I am working on for fun to get more familiar with Perl and I want to work with saving to files, save the word.

I have an array of all the words at the top, I want to be able to SAVE the one random word it chose to a file - Then load it back and just for testing for me to print it?

Any suggestions?

Something like this:

  open (LOG, ">>logfile.txt") || &ErrorMessage;
  print LOG "$comments\n";
  close (LOG);
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
 
# found on google - create new CGI object
my $cgi = new CGI;
print $cgi->header();
 
my @randomwords=("ADULT","AEROPLANE","AIR","AIRCRAFT","AIRFORCE","AIRPORT","ALBUM","ALPHABET","APPLE","ARM", "ARMY","BABY","BABY","BACKPACK","BALLOON","BANANA","BANK","BARBECUE","BATHROOM","BATHTUB","BED","BED","BEE","BIBLE","BIBLE","BIRD","BOMB","BOOK","BOSS","BOTTLE","BOWL","BOX","BOY","BRAIN","BRIDGE","BUTTERFLY","BUTTON","CAPPUCCINO","CAR","CARTWHEEL","CARPET","CARROT","CAVE","CHAIR","CHESS","CHIEF","CHILD","CHISEL","CHOCOLATES","CHURCH","CHURCH","CIRCLE","CIRCUS","CIRCUS","CLOCK","CLOWN","COFFEE","SHOP","COMET","COMPACT","COMPASS","COMPUTER","CRYSTAL","CUP","CYCLE","DATABASE","DESK","DIAMOND","DRESS","DRILL","DRINK","DRUM","DUNG","EARS","EARTH","EGG","ELECTRICITY","ELEPHANT","ERASER","EXPLOSIVE","EYES","FAMILY","FAN","FEATHER","FESTIVAL","FILM","FINGER","FIRE","FLOODLIGHT","FLOWER","FOOT","FORK","FREEWAY","FRUIT","FUNGUS","GAME","GARDEN","GAS","GATE","GEMSTONE","GIRL","GLOVES","GOD","GRAPES","GUITAR","HAMMER","HAT","HIEROGLYPH","HIGHWAY","HOROSCOPE","HORSE","HOSE","ICE","ICECREAM","INSECT","FIGHTER","JUNK","KALEIDOSCOPE","KITCHEN","KNIFE","LEATHER","JACKET","LEG","LIBRARY","LIQUID","MAGNET","MAN","MAP","MAZE","MEAT","METEOR","MICROSCOPE","MILK","MILKSHAKE","MIST","MONEY","MONSTER","MOSQUITO","MOUTH","NAIL","NAVY","NECKLACE","NEEDLE","ONION","PAINTBRUSH","PANTS","PARACHUTE","PASSPORT","PEBBLE","PENDULUM","PEPPER","PERFUME","PILLOW","PLANE","PLANET","POCKET","POTATO","PRINTER","PRISON","PYRAMID","RADAR","RAINBOW","RECORD","RESTAURANT","RIFLE","RING","ROBOT","ROCK","ROCKET","ROOF","ROOM","ROPE","SADDLE","SALT","SANDPAPER","SANDWICH","SATELLITE","SCHOOL","SEX","SHIP","SHOES","SHOP","SHOWER","SIGNATURE","SKELETON","SLAVE","SNAIL","SOFTWARE","SOLID","SPACE","SHUTTLE","SPECTRUM","SPHERE","SPICE","SPIRAL","SPOON","SPORTS-CAR","SPOTLIGHT","SQUARE","STAIRCASE","STAR","STOMACH","SUN","SUNGLASSES","SURVEYOR","SWIMMING","SWORD","TABLE","TAPESTRY","TEETH","TELESCOPE","TELEVISION","TENNIS","THERMOMETER","TIGER","TOILET","TONGUE","TORCH","TORPEDO","TRAIN","TREADMILL","TRIANGLE","TUNNEL","TYPEWRITER","UMBRELLA","VACUUM","VAMPIRE","VIDEOTAPE","VULTURE","WATER","WEAPON","WEB","WHEELCHAIR","WINDOW","WOMAN","WORM","ZEBRA");
my $word = $cgi->param("word");
my $guesses;
 
# finished: 0=no, 1=win, 2=lose
# basing on boolean
my $finished = 0;
 
 
if (!$word){
      #Start new game
      $word = $randomwords[rand($#randomwords)+1];
      $guesses='';
}
else {
      #Play existing game
      
      #Get existing guesses
      $guesses=$cgi->param('guesses');
      
      #If player guessed a new letter, add it to guesses
      if($cgi->param('letterguess')) {
            $guesses .= $cgi->param('letterguess');
      }
      
      #If player guessed a word, check to see if they win, add a guess to guesses
      elsif($cgi->param('guesstheword')) {
            $guesses .= '~';  #Any character not a letter
            if(lc($cgi->param('wordguess')) eq lc($word)) {
                  $finished = 1;
            }
      }
}
 
#Count number of wrong guesses
my $numberwrongguesses = 0;
foreach (split(//, $guesses)) {
      $numberwrongguesses++ unless $word =~ /$_/;
}
 
# Get word with unguessed letters blanked
# It looks for any character that is not a space or in the string $guesses - reg ex below
my $wordHidden = $word;
$wordHidden =~ s/[^ $guesses]/_/g;
 
#Check to see if user guessed entire word
$finished=1 unless $wordHidden =~ /_/;
 
#If not finished, and to many guesses, player loses
$finished=2 if(!$finished and ($numberwrongguesses>8));
 
#If won or lost, show page
if($finished==1) {
      print <<END_HTML_WIN;
<html>
      <head><title>Hangman</title></head>
      <body>
            YOU WIN<br>
            <a href="hangman.cgi">play again</a>
      </body>
</html>
END_HTML_WIN
exit;
}
elsif($finished==2) {
      print <<END_HTML_LOSE;
<html>
      <head><title>Hangman</title></head>
      <body>
            YOU LOSE<br>
            <a href="hangman.cgi">play again</a>
      </body>
</html>
END_HTML_LOSE
exit;
}
 
my $wordPrint = join(" ", split(//, $wordHidden));
 
#Show normal page
print <<END_HTML_1;
<html>
      <head><title>Hangman</title></head>
      <body>
            <h1 align='center'>Hangman</h1>
            <div align='center'>
                  <img src='http://elan.champlain.edu/~rcoughlin32001/images/hangman$numberwrongguesses.jpg' height='250' width='250'><br>
                  $wordPrint <br>
                  <form method='post' action ='hangman.cgi'>
                        <input type="hidden" name="word" value="$word" />
                        <input type="hidden" name="guesses" value="$guesses" />
END_HTML_1
foreach ('A'..'Z') {
      if($guesses =~ /$_/) {
            print qq[$_];
      }
      else {
            print qq[<input name="letterguess" type="submit" value="$_" />]
      }
}
my $output = <<HTML_END;
                        <p>Or guess the word</p>
                        <input name='wordguess' type='text' size='30' maxlength='60' />
                        <input name='guesstheword' type='submit' value='guess word' />
                  </form>
            </div>
      </body>
</html>
HTML_END
 
print $output;
[+][-]11.26.2007 at 05:51AM PST, ID: 20349756

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 06:58AM PST, ID: 20350208

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 06:59AM PST, ID: 20350216

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:00AM PST, ID: 20350227

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:01AM PST, ID: 20350236

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:03AM PST, ID: 20350249

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:03AM PST, ID: 20350251

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:04AM PST, ID: 20350257

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:04AM PST, ID: 20350259

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:07AM PST, ID: 20350274

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:08AM PST, ID: 20350278

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:09AM PST, ID: 20350291

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:11AM PST, ID: 20350300

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Perl Programming Language
Sign Up Now!
Solution Provided By: evilrix
Participating Experts: 3
Solution Grade: A
 
 
[+][-]11.26.2007 at 07:13AM PST, ID: 20350308

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:17AM PST, ID: 20350335

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:19AM PST, ID: 20350360

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:22AM PST, ID: 20350385

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:26AM PST, ID: 20350406

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:28AM PST, ID: 20350424

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:28AM PST, ID: 20350431

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:29AM PST, ID: 20350434

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2007 at 07:29AM PST, ID: 20350435

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628