Arathalion Posted February 24, 2005 Share Posted February 24, 2005 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 More sharing options...
SlimShady Posted February 24, 2005 Share Posted February 24, 2005 Check the Assign() function.When you want to generate/create a variable you need to know the name. Link to comment Share on other sites More sharing options...
CyberSlug Posted February 24, 2005 Share Posted February 24, 2005 You might want to use arrays instead. Look at "Dim" and "ReDim" in the help file Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig! Link to comment Share on other sites More sharing options...
Arathalion Posted February 24, 2005 Author Share Posted February 24, 2005 Check the Assign() function.When you want to generate/create a variable you need to know the name.<{POST_SNAPBACK}>thankyou very much . 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 endfuncideally, 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 More sharing options...
phillip123adams Posted February 24, 2005 Share Posted February 24, 2005 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 More sharing options...
Arathalion Posted February 24, 2005 Author Share Posted February 24, 2005 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 More sharing options...
Arathalion Posted February 24, 2005 Author Share Posted February 24, 2005 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 More sharing options...
SlimShady Posted February 24, 2005 Share Posted February 24, 2005 You should try arrays. Search for "arrays" in this forum for examples. Link to comment Share on other sites More sharing options...
Blue_Drache Posted February 24, 2005 Share Posted February 24, 2005 (edited) 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 loopsDim $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") EndIfThink 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 February 24, 2005 by Blue_Drache Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache Link to comment Share on other sites More sharing options...
phillip123adams Posted February 24, 2005 Share Posted February 24, 2005 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 More sharing options...
ZeDMIN Posted February 24, 2005 Share Posted February 24, 2005 thankyou very much . 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 endfuncideally, 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 ;==>_makevarThough i would suggest an array, too. Link to comment Share on other sites More sharing options...
Arathalion Posted February 24, 2005 Author Share Posted February 24, 2005 Thankyou phillip123adams, ZeDMIN. i didn't know that "eval" function - but that works now. thankyou Blue_Drache for that very useful and informative explanation - i now understand arrays! (well, to an extent) not sure which method i'll use though. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now