in reply to Re^2: Resetting variables
in thread Resetting variables

The particular bug that's causing the error message you're referring to is in the reset_game() sub.

The problem is that you're assigning a single scalar value '0' to a list. That means that $all_occurrences becomes undefined. Use an undefined value within a character class in a regexp and you get an error message. Try the followng:

perl -e "my $var; 'string'=~/[$var]/"

And here's the error...

Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ]/ at -e line + 1.

If you're getting warnings when running under warnings and strictures, the goal is to understand why. The warnings are telling you something. In this case, you mentioned getting uninitialized value warnings. Instead of fixing the problem, you subverted the warnings, and that allowed bugs to creep in. If you had dug into why you were getting those undefined value warnings, you would have tracked the problem down to your reset sub.


Dave

Replies are listed 'Best First'.
Re^4: Resetting variables
by yacoubean (Scribe) on Dec 21, 2004 at 18:04 UTC
    Oops! I pasted an older version of my code...at one point I had that sub coded like this, but went to this incorrect way (as I now understand) to try to fix bugs.
    sub reset_game { ($good_guesses, $bad_guesses) = ""; $tot_found = 0; $found_one = 0; $occurrences = 0; $all_occurrences = 0; $guessed_word = 0; }

      Oops indeed. I spent time on that for you.

      Once again I don't know what your current code looks like. The code you posted above does give the error message you described, for the reason I described. Your newly posted reset_game() sub will not generate the same error, but still contains a bug. Hint: Why are you putting [$all_occurrences] within a character class to check what the user entered, when you've assigned a value of '0' to $all_occurrences? What is that supposed to do?


      Dave

        If you look at my reply here I posted my entire script. $all_occurences is being populated in the check_guess() sub. It keeps a list of index values in the word the player is trying to guess so that when I am printing out the word, I print all of the guesed letters.