Ryan Dillinger wrote: > Hello all, Hello,
> 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; > 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); x is a multiplication operator so the list (0) is multiplied by the number of elements in @letters giving @blankword the same number of elements all with the value of the list. The scalar() is superfluous because @letters is already in scalar context. > 15. OUTER: > 16. while ($wrong < @hangman) { > 17. foreach $i (0..$#letters) { The range operator .. creates a list starting at 0 to the number contained in $#letters which is the index of the last element in @letters. If @letters contains the list ( 'a', 'b', 'c', 'd', 'e' ) then the first element has the index 0 and the last element has the index 4 so $#letters will contain 4. foreach iterates over the list and stores each element in turn into the $i variable. > 18. if ($blankword[$i]) { if tests whether the contents of $blankword[$i] is true or false. The @blankword array was initialised to all zeros which are false. > 19. print $blankword[$i]; > 20. } else { > 21. print "-"; print the '-' character. > 22. } > 23. } > 24. print "\n"; > 25. > 26. if ($wrong) { > 27. print @hangman[0..$wrong-1] print the array slice @hangman[0..$wrong-1] which is the list of $hangman[0] through $hangman[$wrong-1] inclusive. The if test is superfluous because if $wrong-1 is less than 0 then the empty list will be returned to print. > 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) { join joins all the elements of @blankword together separated by the '' string and returns a scalar which is compared to the scalar $answers. > 44. print "You got it right!\n"; > 45. exit; > 46. } > 47. } > 48. print "$hangman\nSorry, the word was $answers.\n"; A more perlish and shorter version. :-) #!/usr/bin/perl # hangman.pl use warnings; use strict; my @words = qw( internet cyber groups information ); my @hangman = qw( 0 - | - - < ); my @letters = split //, $words[ rand @words ]; my @blankword = ( 0 ) x @letters; my %guesses; my $wrong = -1; while ( $wrong < $#hangman ) { print map( $blankword[ $_ ] || '-', 0 .. $#letters ), "\n", @hangman[ 0 .. $wrong ], "\n", ' Your Guess: '; chomp( my $guess = <STDIN> ); $guesses{ $guess }++ and next; $wrong += ! map $letters[ $_ ] eq $guess ? ( $blankword[ $_ ] = $guess ) : (), 0 .. $#letters; die "You got it right!\n" if "@letters" eq "@blankword"; } print @hangman, "\nSorry, the word was ", @letters, ".\n"; __END__ Any more questions? ;-) John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>