Interesting variable strangeness

Previous topic - Next topic

MrTAToad

As you can see in the following code :

Code (glbasic) Select
FUNCTION buildClientListToSend$:toAddName$="",toAddIP%=0
LOCAL temp$

temp$=""
FOREACH loop IN self.playerList[]
INC temp$,loop.name$+SEPERATOR$+loop.ipAddress%+SEPERATOR$+loop.isHost%+SEPERATOR$+loop.isReady%+SEPERATOR$
NEXT

IF toAddName$<>"" THEN INC temp$,toAddName$+SEPERATOR$+toAddIP%+SEPERATOR$+FALSE+SEPERATOR$+FALSE+SEPERATOR$
DEBUG temp$+"\n"

RETURN LEFT$(temp$,LEN(temp$)-1)
ENDFUNCTION


The variable loop isn't defined, nor is it a global variable - and I have explicit declarations on.  The only reason I think the compiler compiles the program is that loop is defined in another function...

kanonet

No, its a normal behavior. Foreach defines a local variable (for das it the same way) that existis only in the for-next loop, you dont have to define it before. At least, i think its intended, 'cuz it works this way for a long time.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

MrTAToad

Yes, that does sound familiar...  In which case its not strange :)

Moebius

You can check in the temp files to see what's actually happening - temporary local variable is used to store the index, and the actual 'variable' you access is a temporary, localled reference.  It makes sense  8)  This also means I've been creating a lot of pointless variables all this time...
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

MrTAToad

Always better to be safe though :)