Hey, Im new to lua, and im trying to pull something off, I know how to script in ACtionScript (flash) what I need know how to do in lua, so somehthing like this (code writing in AS)
| Code: |
for (i=1;i<=5;i++) {
["string"+i] = i
}
|
so that it would make
string1 = 1, string2 = 2 ... ect, easy in flash, but not sure how to do in lua
What you want is probably a hashtable (or associative array) which I'm not sure how is implemented in Lua. To me it looks like you're trying to implement dynamic variables, ie. variables that get names dynamically. Is this correct? I didn't know it was possible to assign a string literal to something in any language, but a hashtable will do just the same, and will make the whole thing a lot cleaner.
In python:
| Code: |
strings = dict() #Alternatively strings = {}
for i in range(1, 6):
strings["string%d" % i] = i
print strings
#Returns {'string4': 4, 'string5': 5, 'string2': 2, 'string3': 3, 'string1': 1}
|
What the code returns will differ on differents computers, as dictionaries are unordered by definition, as indeed most hashtables are. But you can ask for the keys of it ('string1'...) and get it in a list, and sort that list before you output:
| Code: |
for key in sorted(strings.keys()):
print key, ": ", strings[key],
#Returns 'string1': 1 'string2': 2..., and will do on any computer
|
Which means you can gather all your strings in the same object and pass it around to functions more easily than would otherwise have been possible. Of course, if you're doing something as simple as the example you used, there's probably no use for this, you can just pass the minumum and maximum (in this case 1 and 5) to any function that will use this. I don't know how much help this is, but I've heard that Lua is quite similar to Python, so I hope it is of some help, at least.
Thanks for the tip, I am trying to get var names dynamically. As it is simple in ActionScript (what I posted above) I was hoeping it would be as easy as what i posted, but i guess all scripting languges cant all be easy ^_~. Ill see if I can get what you posted to work with what i need, (will be about a day) Ill post back saying if i got it to work or not, thnx
Couldn't you just use an array?
an array dosnt have antying to do with what I need, im trying to get var names dynamics,
AKA the var that I need, will be given to me by another var + a string.
so lets say
var test = "23"
["myvar"+test+"hi"]._x
Would give me the X value of the var "myvar23hi" in flash, wish is what im trying to do in lua.
For this, you really, really want to use a hashtable, nothing else. Curiously enough, a lot of interpreted languages implement the variables in their interpreter as keys in hashtables. Python uses two, a local and a global hashtable (dictionary), so a variable is simply another key in the hashtable. This goes a long way to show how neat these are for doing things like creating dynamic variable names and things like that. It also has the advantage that you can easily pass all the variables you have created to functions or methods at once, instead of passing one by one or some other such thing.
| m-productions wrote: |
Hey, Im new to lua, and im trying to pull something off, I know how to script in ACtionScript (flash) what I need know how to do in lua, so somehthing like this (code writing in AS)
| Code: |
for (i=1;i<=5;i++) {
["string"+i] = i
}
|
so that it would make
string1 = 1, string2 = 2 ... ect, easy in flash, but not sure how to do in lua |
I think this is what you are looking for:
| Code: |
for i=1,9 do _G["string"..i]=i end
|
| Code: |
> for i=1,9 do _G["string"..i]=i end
> = string1
1
> = string2
2
> = string3
3
>
|
You can use _G[varname to access global variables using their name. For example:
Assigns 10 to the variable a.
For more information: http://www.lua.org/pil/14.1.html