# Help With variable

## Recommended Posts

Hey

My first topic here, seems like a nice forum (have used it much as a guest)

Now I have a problem with a script and think its quite basic, but dunno how to solve it.

I have a part of my script (cut down to highlight the problem):

#############

Run(\$File1)

..

..

..

Run(\$File2)

..

..

..

Run(\$File3)

..

..

..

ect

ect

##############

So instead of making script for each variable which are the same, only differs in variable, I want it to be like this:

Run(\$File1)

..

..

..

I have an ini file for all the \$file and \$Load so I want it to optional how many I want to read. Dunno if I explained my problem, for you to understand, else I try to reexplain.

##### Share on other sites

Have a look at Arrays in the helpfile.

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

sponk,

Now that you've read the stuff as advised by the heavy hitters, here's some code to chew on.

```;
; the following demonstrates looping through predefined variables using eval
;

local \$var1=10,\$var2=11,\$var3=12,\$var4=13,\$var5=14,\$var6=15,\$var7=16,\$var8=17,\$var9=18,\$var10=19

for \$i = 1 to 10
c(eval('var' & \$i) & ' i = ' & \$i)
if eval('var' & \$i) = 15 then c('hit on \$var6')
next

;
; the following demonstrated looping through a 2X array
;

local \$a10[10][2] = [['act1_1','act1_2'], _
['act2_1','act2_2'], _
['act3_1','act3_2'], _
['act4_1','act4_2'], _
['act5_1','act5_2'], _
['act6_1','act6_2'], _
['act7_1','act7_2'], _
['act8_1','act8_2'], _
['act9_1','act9_2'], _
['act10_1','act10_2']]

for \$i = 0 to ubound(\$a10,1) - 1
c('invoking action = ' & \$a10[\$i][0] & ' followed by action ' & \$a10[\$i][1])
next

func c(\$str)
consolewrite('> ' & \$str & @lf)
EndFunc```

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

##### Share on other sites

Thanks all for very quick replies. Now I know what to read about to make it work. Maybe I get back if I cant figure it out.

Regards

##### Share on other sites

If you really have all the values ahead of time, then walking through an array of values is more efficient, as mentioned.

If you use various values at different points in the script though, you could just turn it into a function:

```_MyFunc(\$File1, \$Load1)

Run(\$sFile)
; ..
; ..
; ..
EndFunc```

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

##### Share on other sites

You can use arrays or use variable functions as this example

example

```For \$i = 1 To 5

If Assign("variable" & \$i , "Hello" & \$i ) Then MsgBox(4096, "", Eval("variable" & \$i ))

Next

MsgBox(4096, "", _
Eval("variable1") & @CRLF & _
Eval("variable2") & @CRLF & _
Eval("variable3") & @CRLF & _
Eval("variable4") & @CRLF & _
Eval("variable5"))```

صرح السماء كان هنا

##### Share on other sites

sponk,

Since you mentioned that you have the value pairs in an "ini" file and that you want to "optionally" run these pairs, psalty's solution with rudimentary selection logic makes the most sense.

|

|

execute pair? ---> yes call psalty's function

|

| no

|

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

## Create an account

Register a new account

×

• Wiki

• Back

• Git