in reply to hangman question

$#letters is the index of the last element in @letters (or -1 if there are no elements). $blankword[$i] accesses the element at index $i of @blankword. The loop foreach $i (0..$#letters) {...} prints each of the known letters in the word and prints a '-' for any unknown letters. The if ($blankword[$i]) tests to see if a letter is known or unknown by testing to see if the element is zero or not.

In scalar context @array returns the number of elements in @array (cf $#array above). So $guesses[@guesses]=$guess; assigns $guess to the next unused element in @guesses. It would be more Perlish to push @guesses, $guess instead.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: hangman question
by convenientstore (Pilgrim) on Jul 16, 2007 at 00:37 UTC
    Hey, thanks for your reply. Further questions on your explanation..
    previsouly, "@blankword" was 0 0 0 (let's say for 3 letter words.. say like ---> n o w

    so going through (0..$#letters), $i gets assigned n
    o
    w
    now, how does $blankwordn equals element is zero or not?

      No. $i gets assigned 0, 1 and 2 - indexes into the two arrays, not the contents of the elements of @letters. 0 .. $#letters is the list context range operator and generates a list containing the integers from 0 to the index of the last element in @letters (0, 1 and 2 for this example).

      The code depends on elements of @blankwords being set to 0 until they are assigned a correct letter. The test could be rewritten if ($blankword[$n] eq $letters[$i]) { to make an explicit check that the correct letter has been guessed.


      DWIM is Perl's answer to Gödel
        I think now I am understanding the script. However, I ended up commenting out the @guesses portion (mainly because I think @guesses is useless part and script still works)..
        Was author wrong to put that code there(since it does nothing?).
        #!/usr/bin/perl -w @words = qw( hi no ); @guesses=(); $wrong=0; $choice=$words[rand @words]; $hangman="0-|--<"; @letters=split(//, $choice); print "\@letters is @letters\n"; @hangman=split(//, $hangman); @blankword=(0) x scalar(@letters); print "\@blankword is @blankword\n"; OUTER: while ($wrong<@hangman) { print "whielloop\n"; foreach $i (0..$#letters) { if ($blankword[$i]) { print $blankword[$i]; } else { print "-"; } } print "\n"; if ($wrong) { print @hangman[0..$wrong-1] } print "\n\n\n\n Your Guess: "; $guess=<STDIN>; chomp $guess; # foreach(@guesses) { # print "foreach loop\n"; # print "\$guess is $guess\n"; # print "\$\_ is $_\n"; # next OUTER if ($_ eq $guess); # } # print "\@guesses is @guesses\n"; # $guess_scalar = "@guesses"; # print "\$guess_scalar is $guess_scalar\n"; #$guesses[@guesses]=$guess; # push @guesses, $guess; $right=0; for ($i=0; $i<@letters; $i++) { print "\$i is $i\n"; print "in the forloop\n"; if ($letters[$i] eq $guess) { $blankword[$i]=$guess; print "\$blankword\[\$i\] is $blankword[$i] +\n"; $right=1; } } $wrong++ if (not $right); print "\$wrong is $wrong\n"; if (join('', @blankword) eq $choice) { print "IF loop\n"; print "You got it right!\n"; exit;