Jump to content

Dynamically naming variables


Recommended Posts

this could be a stupid question, but i can't figure it out from the help file or forum search, so..

i want to create some dynamically named variables, for example $var1, $var2, $var3 and so on. but i want to do that without explicitly declaring them because that would take lots of code. is this possible?

heres's my attempt - hopefully this illustrates what i'm trying to acheive:

func makevar($foo)
global "$var" & $foo
endfunc

i also want the function to return the name of the variable that it has just created, but i have no idea how i'd do that. any ideas?

Link to comment
Share on other sites

Check the Assign() function.

When you want to generate/create a variable you need to know the name.

<{POST_SNAPBACK}>

thankyou very much :lmao:. somehow i never saw that function before....

OK, now i can't seem to return the data in the string, because the name is a string...

for $bob = 1 to 20
$yarr = makevar($bob)
msgbox (0,$yarr,$yarr)
next
exit

func makevar($foo)
assign ("var" & $foo, "hello" & $foo,2)
return "$var" & $foo
endfunc

ideally, i'd like the message boxes to have the name of the variable as a title, and the contents of the variable as the message. but i can't seem to get the contents of the variable.

Link to comment
Share on other sites

i want to create some dynamically named variables, for example $var1, $var2, $var3 and so on. but i want to do that without explicitly declaring them because that would take lots of code. is this possible?

i also want the function to return the name of the variable that it has just created, but i have no idea how i'd do that. any ideas?

<{POST_SNAPBACK}>

Here the code. Unfortunately, the AutoIt3 Syntax Checker throws warnings and errors because it does not see variables created in this manner. On the other hand, AutoIt3 handles it just fine.

For $i = 1 to 9
   $x = makevar($i)
   MsgBox(4096, "MakeVar Result", $x)
Next
MsgBox(4096, "MakeVar result", $var1 &@lf& $var2 &@lf& $var3 &@lf& $var4 &@lf& $var5)
Exit

func makevar($foo)
   assign("var" & $foo, $foo + 100, 2)
   Return("$var" & $foo)
endfunc

Phillip

Link to comment
Share on other sites

You might want to use arrays instead.

Look at "Dim" and "ReDim" in the help file

<{POST_SNAPBACK}>

i'm sorry but i don't know what an array is; i'm no programmer, i just know a little bit of autoit.

i've looked at the Dim/ReDim functions, but they presume that you know what an array is, which i don't.

but thankyou very much all the same.

Link to comment
Share on other sites

Here the code.  Unfortunately, the AutoIt3 Syntax Checker throws warnings and errors because it does not see variables created in this manner.  On the other hand, AutoIt3 handles it just fine.

For $i = 1 to 9
   $x = makevar($i)
   MsgBox(4096, "MakeVar Result", $x)
Next
MsgBox(4096, "MakeVar result", $var1 &@lf& $var2 &@lf& $var3 &@lf& $var4 &@lf& $var5)
Exit

func makevar($foo)
   assign("var" & $foo, $foo + 100, 2)
   Return("$var" & $foo)
endfunc

<{POST_SNAPBACK}>

thankyou for writing this, but it still has the problem of having to type out all the variables at one point:

MsgBox(4096, "MakeVar result", $var1 &@lf& $var2 &@lf& $var3 &@lf& $var4 &@lf& $var5)

i need to have that dynamic, if that's possible. the program that it's for could have hundreds of variables, and i can't have that all typed out!

Link to comment
Share on other sites

i'm sorry but i don't know what an array is; i'm no programmer, i just know a little bit of autoit.

i've looked at the Dim/ReDim functions, but they presume that you know what an array is, which i don't.

but thankyou very much all the same.

<{POST_SNAPBACK}>

Array:

Dim $foo[100]

Now we have 100 containers within the single variable $foo, numbered 0 to 99.

$foo[3] is separate from $foo[56]

Using arrays allows us to put single variables into loops

Dim $x = 0
Dim $foo[100]
$file = FileOpen("C:\file.txt",0)

For $x = 0 to 99 step 1
; read a line from a text file and put it in $foo
   $foo[$x] = FileReadLine($file)
      If @error = -1 then ExitLoop
Next

If $x >= 100 then 
   MsgBox(0,"Info","The first 100 lines of the file have been read")
Else
   MsgBox(0,"Info","The file contained " & $x & "lines and it is stored in the array $foo")
EndIf

Think of an array as a warehouse with a limited ammount of shelf space. Each shelf is numbered, starting at ZERO..... remember that, your last container is always one less than what you Dim-ed the array.

Edit: forgot the

Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Link to comment
Share on other sites

thankyou for writing this, but it still has the problem of having to type out all the variables at one point:

MsgBox(4096, "MakeVar result", $var1 &@lf& $var2 &@lf& $var3 &@lf& $var4 &@lf& $var5)

i need to have that dynamic, if that's possible. the program that it's for could have hundreds of variables, and i can't have that all typed out!

<{POST_SNAPBACK}>

Okay, but that's a different requirement from the original post which only wanted the variable name. Try this:

For $i = 1 to 9
   $x = makevar($i)
   MsgBox(4096, "MakeVar Result", $x & " = " & eval(stringmid($x, 2)) &@lf&@lf& "or" &@lf&@lf& 'eval("VAR" & $i) = ' & eval("var" & $i))
Next
Exit

func makevar($foo)
   assign("var" & $foo, $foo + 100, 2)
   Return("$var" & $foo)
endfunc

Phillip

Link to comment
Share on other sites

thankyou very much :lmao:. somehow i never saw that function before....

OK, now i can't seem to return the data in the string, because the name is a string...

for $bob = 1 to 20
$yarr = makevar($bob)
msgbox (0,$yarr,$yarr)
next
exit

func makevar($foo)
assign ("var" & $foo, "hello" & $foo,2)
return "$var" & $foo
endfunc

ideally, i'd like the message boxes to have the name of the variable as a title, and the contents of the variable as the message. but i can't seem to get the contents of the variable.

<{POST_SNAPBACK}>

Umm, how about this:

For $i = 1 To 20
    $x = _makevar($i)
    MsgBox(0, $x, Eval($x))
Next
Exit

Func _makevar($num)
    $var = "var" & $num
    Assign($var, "hello" & $num, 2)
    Return $var
EndFunc  ;==>_makevar

Though i would suggest an array, too.

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...