PDA

View Full Version : The Case of the Vanishing Variable



Blypht
09-24-2008, 05:58 PM
I'm a longtime LW user and LScript programmer, but this one has me baffled.

I have a global variable at the top of my LScript. It's a 1-dimensional array which I am treating as a stack.

The LScript goes merrily along, pushing, popping, and peeking with the stack. And then, all of a sudden, the global variable seems no longer to be defined, as if it has passed out of scope. If I try to use a variable message (such as .count()) with it, it crashes Modeler.

I have traced it right up to the moment it dies, but can't figure out why the variable is vanishing.

Has anyone else had a similar experience? Any ideas or suggestions?

Thanks in advance!

evenflcw
09-25-2008, 05:34 AM
I've had similar things happen. Most of the times it was my code that was erroneous. Either I hadn't treated all cases properly or missed one (ie. create array from scratch; array was n size, add one; array got emptied, add one. and so on) or I made incorrect assumptions (or just plain ignorance) on how array variables work in lscript. Often I suppose the array got turned into a simple variable or set to nil, in which in which case reading it would not work and count() might be undefined. So make sure all assignments use the square brackets and treat the array variable like an array variable all the time and make sure you never do something that can invalidate the array (set it to nil, ie completely empty without some form of counter measure, ie design better). Or perhaps explicitly declare the array using the var keyword (I never used it).

Associative arrays don't seem to like variable methods like pack() and such. I've been meaning to report that but never gotten around to it.

I wrote a "generic" stack "interface" (as in java interface) that turned out pretty well I think. All you have to do is "implement" the interface (was just an enum if I remember correctly) and you should be set. I might post that.

Blypht
09-25-2008, 12:23 PM
Just to cross my t's and dot my i's I went back and rechecked every reference. They're all right. I'll keep looking....

You'd think that with a third of a century of professional software development I'd be immune to this sort of snafu. Murphy's Law, I guess.

Thanks for your input.