in reply to Re: Resetting variables
in thread Resetting variables

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:

my $var = 0; while (condition) { # read var; gets changed by sub2 on occasion sub1() sub2() } sub1 { # read $var; gets changed by sub2 on occasion } sub2 { # update $var }

Replies are listed 'Best First'.
Re^3: Resetting variables
by Sandy (Curate) on Dec 21, 2004 at 22:09 UTC
    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.