Sign in to follow this  
Followers 0
evilertoaster

automatic loop scopes

7 posts in this topic

Hum I'm finding so many quirks latley in autoit... Ok how bout this-

For $n=1 to 2
    _Foo()
Next

Func _Foo()
    $n=0
EndFunc

Infinite loop...

But the $n in the for loop should be local... so why does the _Foo() function change it?

Share this post


Link to post
Share on other sites



$n is being declared as global by the For statement. Use Local inside the function to stop the infinite loop.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

$n is being declared as global by the For statement. Use Local inside the function to stop the infinite loop.

I had a similar issues with this, best bet is to use a different variable other than n, maybe $i

Global $i[8]

Func _Main()

$i = blah

EndFunc

While 1

Dim $i

GUICtrlRead($i, blah...)

WEnd

This will cancel out $i since it is being claimed Dim, after global. It overrides the $i in the function even though it's global because of the Dim..

Sorry about my ill way of explaining it if you don't understand.

Edited by Anonymouse

children may smile; the wise ponder- Dr. Holmes of Hardvard Medical School on an Ether BingeLove Makes The World Go Round?So does five shots of tequila. What's your point?[quote name='Valik' date='Jun 5 2008, 05:13 PM']wraithdu, 24 hours. Said I have a bad attitude, just driving the point home with a ban.[/quote]This is classic. :)

Share this post


Link to post
Share on other sites

$n is being declared as global by the For statement. Use Local inside the function to stop the infinite loop.

But from the helpfile-

Loop based on an expression.

For <variable> = <start> To <stop> [step <stepval>]

statements

...

Next

Parameters

variable The variable used for the count.

start The initial numeric value of the variable.

stop The final numeric value of the variable.

stepval [optional] The numeric value (possibly fractional) that the count is increased by each loop. Default is 1.

Remarks

The Variable will be created automatically with a LOCAL scope, even when MustDeclareVars is on.

Share this post


Link to post
Share on other sites

I had a similar issues with this, best bet is to use a different variable other than n, maybe $i

Global $i[8]

Func _Main()

$i = blah

EndFunc

While 1

Dim $i

GUICtrlRead($i, blah...)

WEnd

This will cancel out $i since it is being claimed Dim, after global. It overrides the $i in the function even though it's global because of the Dim..

Sorry about my ill way of explaining it if you don't understand.

I know perfectly well what its doing and how to aviod it...I'm woundering more-so why it's doing that, in particular if it's intened or always been that way...

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

For $x=1 to 1
    $Result = IsDeclared("x")
    If $Result = 1 Then
        ConsoleWrite("Variable is global" & @CRLF)
    ElseIf $Result = -1 Then
        ConsoleWrite("Variable is local" & @CRLF)
    EndIf
Next

_Foo()

Func _Foo()
    For $n=1 to 1
        $Result = IsDeclared("n")
        If $Result = 1 Then
            ConsoleWrite("Variable is global" & @CRLF)
        ElseIf $Result = -1 Then
            ConsoleWrite("Variable is local" & @CRLF)
        EndIf
    Next
EndFunc

Variable is global
  Variable is local

Documentation is wrong.

The documentation for IsDeclared() says:

Returns 1 for Global variable or variable declared outside functions.

Edited by weaponx

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

So that tells me it doesn't create a local variable for For-Next loops if the For-Next is not in a function, or (without double negatives), For-Next will only create a local variable if the For-Next is inside a function.

Maybe a slight wording change to the help file is in order?

Edited by evilertoaster

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0