haybail Posted August 15, 2008 Posted August 15, 2008 How do you name a variable the name of another variable so to copy it down? On the script. Say I have one of these: crazies = { ["one"] = 500, ["two"] = 5000, ["three"] = 50000 } or this: crazies = [10] = 5, [4] = 2, [1] = .5 } or even this: local x,y,z, = getElementPosition ( theElement) crazies = { [x], [y], [z] } Not sure if you can do that but anyway. And I want to call the variable and write it down as its name in the crazies. How do I do that? for k,v in pairs(crazies) do ( the name of the variable in crazies ) = theValue end outputChatBox ( one ) outputChatBox ( 10 ) outputChatBox ( x ) I tried tostring().
Gamesnert Posted August 15, 2008 Posted August 15, 2008 Also tried to do the same thing, but I failed too. I think it's not possible.
50p Posted August 15, 2008 Posted August 15, 2008 I don't think I know what you're trying to do. If you name a "cell" of table it's not located at index, that is if your table looks like: table[ "one" ], table[ "two" ]... table[ 1 ] does not exist in the table unless you made it. So, for example: local tab = { a = 1, b = 10 } for k, v in pairs( tab ) do print( k, type( k ) ) end -- this will print: -- a string -- b string As you can see a and b are strings. In the 3rd example you can do this: crazies = { getElementPosition( theElement ) }
Gamesnert Posted August 15, 2008 Posted August 15, 2008 I think he means the first thing. That was what I wanted to find out too. Thanks!
haybail Posted August 16, 2008 Author Posted August 16, 2008 So it will print the string out to chatbox? Pretend its: # local tab = { apple = 1, banana = 10 } It will print apple, banana? I'm trying to extract the variable from the table and take it in its simplest form so for example: table = { for = true, fabalaba = 10 } Would be table[for] and table[fabalaba]. I'm trying to get it to be for and fabalaba with the same values by extracting it and making the new variables from the table. Can I do that with print? Maybe: print( k, type( k ) ) = v or tostring(v)
50p Posted August 16, 2008 Posted August 16, 2008 You can simply: local tab = { apple = 1, banana = 10 } print( tab.apple ) -- this will print 1 to server console print( tab.banana ) -- and this will print 10 tab.banana = 11 -- let's change value of banana in table print( tab.banana ) -- now it will print 11 To read more about Lua visit their official website: - http://www.lua.org/pil/index.html - http://www.lua.org/manual/5.1/ And some wiki: - http://lua-users.org/wiki/
haybail Posted August 16, 2008 Author Posted August 16, 2008 But I want to extract the variable so I can do this: print( apple ) Another example would be like this, but this is sloppy: # local tab = { apple = 1, banana = 10 } function takeFruit ( ) for a,e in pairs ( tab ) if a = apple then apple = e elseif a = banana then banana = e end end end Maybe not sloppy but not how I would like to do it. If I could get the name of the variable I could do this: for a,e in pairs ( tab ) a = e -- ( apple = 1 ) end
50p Posted August 16, 2008 Posted August 16, 2008 I think I finally know what you're trying to do now. LOL You could do something like this: tab = { apple = 1, banana = 10 } for k, v in pairs( tab ) do loadstring( tostring( k ) .." = " .. tostring( v ) )( ) end --[[ after this loop, you will have variables with names of "cells" from the tab so you can use the names as variables, like: ]] print( apple ) print( banana ) If you have some MTA elements or tables in the tab, you can use similar method: tab = { apple = 1, banana = 10 } for k, v in pairs( tab ) do loadstring( tostring( k ) .." = tab.".. tostring( k ) )( ) end print( apple ) print( banana )
tma Posted August 16, 2008 Posted August 16, 2008 Might be more efficient to do: tab = { apple = 1, banana = 10 } for k, v in pairs( tab ) do _G[k] = v end print(apple) print(banana)
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