Sign in to follow this  
Followers 0
Alterego

Should 'for' variables be useful in sub-functions?

9 posts in this topic

#1 ·  Posted (edited)

I seem to have had varying results in the past, although it is possible I was doing something wrong.

As some of you may know by now ( :( ) I am fond of multi-tiered for loops due to the nature of automatically processing files in six languages, of three different types, for two different operating system and then either with or without images.

So assume I have the following structure (which I do) in pseudocode:

$lang = StringSplit('English,German,French,Dutch,Polish,esperanto')
$wik = StringSplit('Wikipedia,Wiktionary,Wikiquote')
$os = StringSplit('PPC,PALM')
$img = StringSplit('TXT,IMG')

For $a = 1 to lang[0]
     For $b = 1 to wik[0]
           For $c = 1 to os[0]
                For $d = 1 to img[0]

                      logfile()
                      Do a whole bunch of stuff tons of times over
                      logfile()


                 Next
           Next
      Next
Next

Func logfile()
        _FileWriteLog(@DesktopDir & '\log\' & $lang[$a] & '-' & $wik[$b] & '-' & $os[$c] & '-' & $img[$d] & '.txt')
EndFunc

This does not seem work with my loop, now over 300 lines. A few questions that, if anyone could answer would help me out a lot:

1) What is the actual scope of the for variable (local, global) ?

2) Should it be available outside of the loop in a subfunction?

3) Is it available outside of the subfunction?

Because this does not seem to work for me it seems necessary to not use UDFs and repaste code etc... but that is not ideal.

edit: update code to be more clear

Edited by Alterego

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I never rely on "global" variable if I can at all help it, avoids this problem. I know I'm not answering your scope question, but why not pass the variables to the function?

logfile($a,$b,$c,$d)

*EDIT

I also use Opt("MustDeclareVars",1) in all my scripts, then you know if it's a local or global since you have to declare it.

Edited by Ejoc

Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs

Share this post


Link to post
Share on other sites

Global $a

For $a = 1 to 10


Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs

Share this post


Link to post
Share on other sites

That's the idea I was just having. This throws a scope error so it seems to work:

Local $a = 0

For $a = 1 to 10
    _increment()
Next

Func _increment()
    $a = $a + 1
EndFunc

ClipPut($a)

Share this post


Link to post
Share on other sites

I still say passing variables is better practice, you are incremement $a twice in your example:

Local $a = 0

For $a = 1 to 10 Step 0
    _increment($a)
Next
ClipPut($a)

Func _increment(ByRef $val)
    $val = $val + 1; $val += 1 using beta
EndFunc

Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs

Share this post


Link to post
Share on other sites

I see what you are getting at. It seems that it would guarantee the variables made it outside of the for loop and into the function no matter what. It requires just slightly more complex code but should be worth it in reliability. I'll update..

Share this post


Link to post
Share on other sites

I wonder, what of the possibility of then handling exceptions in the functions themselves by ByRef'ing them the for loops variable and allowing them to increment/decrement them.

And it works. This is great, a never-ending loop:

Global $a

For $a = 1 to 2
    _decrement($a)
Next

Func _decrement(ByRef $a)
    $a = $a - 1
EndFunc

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