Jump to content

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


Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
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
Link to comment
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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...