in reply to Resetting variables

By jove, I think I got it...

Firstly, although I am not certain, I believe the error message is erroneously pointing to the wrong line $guess !~ m/[[:alpha:]]/ rather the error occurs in the elsif portion, where you have

$guess =~ /[$good_guesses]/

During the 1st pass, $good_guesses is either undefined, or set to a single letter (or multiple letter? doesn't matter).

On second pass, the $good_guesses is reset to a null string.

$guess =~ /[$good_guesses]/
gets translates to ...
$guess =~ /[]/
However, if I am not mistaken, character classes will accept a ']' as a valid element of the character class, if it is the first element of the class. So, the parser is taking the ']' as an element of the class, and then continues to look for the closing square bracket.

For whatever reason, if $good_guesses is undefined, the parser figures it out the way you meant. (UPDATE: Nope, that's not the reason, see update below)

To get your code to work, I re-initialized $good_guesses = undef; in your "reset_game" sub.

PS: I wrote my response after I got your program to work, but I did not actually go and read up on all the particulars of character classes in regular expressions, so I may not be entirely accurate.

Sandy

PS (again) ... That was fun!

UPDATE: Made mistake... the reason the code works when $good_guesses is undefined is that there is a condition in front of the condition...

elsif ((defined($good_guesses))&&($guess =~ /[$good_guesses]/))
that prevents the interpretation
$guess =~ /[]/
when $good_guesses is not defined

Replies are listed 'Best First'.
Re^2: Resetting variables
by yacoubean (Scribe) on Dec 21, 2004 at 21:36 UTC
    Hmm, I believe I am checking to make sure $good_guesses is defined (unless I don't understand what defined() really does):
    } elsif ((defined($good_guesses))&&($guess =~ /[$good_guesses]/)) { print "\n***You already guessed that letter!*** (hit any key)\ +n"; <>; } else { if ($word =~ /[$guess]/) { if (defined($good_guesses)) { $good_guesses .= "$guess" unless ($guess =~ /[$good_gu +esses]/); } else { $good_guesses .= "$guess"; }
      Simply put...
      my $var; # variable is undefined if ($var) { ... } # $var is false because it is undefined if (defined $var) {...} # false $var = ""; if ($var) { ... } # false because $var is null string if (defined $var) {...} # true because $var has been # defined as having a null string $var = 0; if ($var) { ... } # false because $var is zero if (defined $var) {...} # true because $var has been # defined as zero
Re^2: Resetting variables
by yacoubean (Scribe) on Dec 21, 2004 at 22:04 UTC
    Yes, you appear to be right Sandy! I comment out two chunks that are doing stuff with $good_guesses, and it works (well, not the way I want, but I don't get the error). Now I have to figure out how to fix that, because it is now doing it on every run, not just the second pass. I don't know why though, because I'm defining $good_guesses = "" at the top.

    A lot of folks have wagged fingers at me for using global variables, and I know what they mean. I just don't know how else to do it! If I want to have a variable that needs to be accessed/updated by the main loop and by multiple subs...how can you do that without storing the variable outside of all the subs? If I try to put it in the loop it will be zeroed out on every loop, and the same with the subs. Any ideas would be appreciated.

    This is what I mean:

      Oops, our posts have crossed in the mail (so to speak).

      Try either

      if ($goodguesses && ($whatever =~ /[$goodguesses]/) {...}
      or undefine $goodguesses
      $goodguesses = undef;

      see previous post (just before your last one, for the differences between 'false' and 'defined'

        AAAhhhh!! You set me straight. ;) Using your clarifications, It now works on the first and second passes! Thanks a bunch.