This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LDDestroier's profile picture

How to get a list of variables declared in tables

Started by LDDestroier, 10 October 2015 - 06:16 PM
LDDestroier #1
Posted 10 October 2015 - 08:16 PM
So say there's a table like this:

doop = {
	one = "One!",
	two = "Tehwoo!",
	three = "Tree!",
}

I want to be able to get a table that ends up like this:

doopMethods = seperateMethods(doop)
doopMethods = {
	{
		"one",
		"One!",
	},
	{
		"two",
		"Tehwoo!",
	},
	{
		"three",
		"Tree!",
	},
}

How do I do this?
Edited on 10 October 2015 - 06:35 PM
Wojbie #2
Posted 10 October 2015 - 08:56 PM
You can achiveve that using

for key,value in pairs(table) do end
loop.

And

table.insert(table,value)
to add stuff to your new table.

In your program case it would be

local function seperateMethods(In)
Out={}
for key,value in pairs(In) do
  table.insert(Out,{key,value})
end
return Out
end

In this code the before-mentioned loop is going over the given table using pairs function and placing all keys with their values in 2nd table.

Can i ask why you are changing from 1st table to 2nd? I mean reasoning for it?
Edited on 10 October 2015 - 07:07 PM
LDDestroier #3
Posted 10 October 2015 - 09:12 PM
Can i ask why you are changing from 1st table to 2nd? I mean reasoning for it?

You mean why I would want to do this? Or why I changed what I wanted halfway? I think the latter.

It's because I realized I needed more information than I originally thought when typing it.

Also, I posted this because I needed help for my STD program.
Wojbie #4
Posted 10 October 2015 - 09:17 PM
You mean why I would want to do this? Or why I changed what I wanted halfway? I think the latter.
I meant former but i guess you want to just store data in different way that makes sense to you.
YoYoYonnY #5
Posted 12 October 2015 - 10:09 PM
Note that, performance wise, working with what you got is always faster than converting to what you want. Not that Lua is that fast though >.>