Ryan Dillinger am Sonntag, 6. August 2006 23:05:
> Hello all,

Hello Ryan

>   I was wondering if someone could explain lines: 14, 17, 18, 21, 27, and
> 43 please. I understand the bulk of the script. But to put it all
> together would better.  Thanks so much!
>
> 1. #!/usr/bin/perl
> 2. # hangman.pl
> 3. use warnings;

Not requested answer:

use strict;
is missing, and the declarations of variables throughout the script.

> 4.
> 5.   @words = qw(internet cyber groups information);
> 6.   @guesses = ();
> 7.   $wrong = 0;
> 8.
> 9.   $answers = $words[rand @words];
> 10. $hangman = "0-|--<";
> 11.
> 12.  @letters = split(//, $answers);
> 13.  @hangman = split(//, $hangman);
> 14.  @blankword = (0) x scalar(@letters);

To find out yourself:
print join ',', @blankword;

@blankword contains as many entries with '0' as there are entries in @letters.

perldoc -f scalar
perldoc perldata


> 15.    OUTER:
> 16.       while ($wrong < @hangman) {
> 17.         foreach $i (0..$#letters) {

$i takes the range 0 to the highest index of @letters.
$#letters == scalar(@letters)-1.

perldoc perldata

> 18.           if ($blankword[$i]) {

true if @blankword contains a true value at index position $i.

> 19.      print $blankword[$i];
> 20.     } else {
> 21.      print "-";

unnecessary use of double quotes ;-)

> 22.     }
> 23. }
> 24.  print "\n";
> 25.
> 26.         if ($wrong) {
> 27.         print @hangman[0..$wrong-1]

This is an array slice: entries at index positions 0..$wrong-1 of @hangman.

perldoc perldata

> 28.}
> 29.print "\n Your Guess: ";
> 30.    $guess = <STDIN>; chomp $guess;
> 31.        foreach(@guesses) {
> 32.           next OUTER if ($_ eq $guess);
> 33.}
> 34. [EMAIL PROTECTED] = $guess;
> 35. $right = 0;
> 36.       for ($i = 0; $i < @letters; $i++) {
> 37.          if ($letters[$i] eq $guess) {
> 38.            $blankword[$i]= $guess;
> 39.               $right = 1;
> 40.   }
> 41.}
> 42. $wrong++ if (not $right);
> 43. if (join('', @blankword) eq $answers) {

Compares the concatenated entries of @blankword with the content of $answers.

perldoc -f join
perldoc perlop

> 44.     print "You got it right!\n";
> 45.     exit;
> 46.   }
> 47. }
> 48. print "$hangman\nSorry, the word was $answers.\n";


Dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to