Short How-to Help

Post Reply
Skylark
Posts: 46
Joined: Mon Feb 22, 2010 12:38 am

Short How-to Help

Post by Skylark »

Perhaps I've just been thinking about this the wrong way, but I've looked over a bunch of material and can't find quite what I'm looking for.

I have defined variables in a script.

apple = 0
banana = 0
cranberry = 0

I have also defined a table with these variables' names contained in it.

fruittable = { "apple", "banana", "cranberry" }

I want to write a function that will take all of the string entries in a table, and use these strings to set the variables (of the same names) equal to 1. I know how to iterate with pairs() and ipairs, so the crux of my question is, how do I use the string returned by fruittable[1] (which is apple) to set a value for the variable of the same name?

Edit: I'm well aware that all the functionality I'm looking for is available in tables defined like the below, instead. In fact that's what I'm doing now, but after the time I spent trying to figure out how to do what I mentioned above I really want to know if it's possible.

fruittable = { apple=0, banana=0, cranberry=0 }

Denarii
Posts: 111
Joined: Thu Dec 03, 2009 10:54 pm

Re: Short How-to Help

Post by Denarii »

Code: [show] | [select all] lua
for i, v in ipairs(fruittable) do
	_G[v] = 1
end

Post Reply