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

Q:About Table.key Manipulation

Started by zilla_CN, 02 January 2015 - 03:43 PM
zilla_CN #1
Posted 02 January 2015 - 04:43 PM
Hi guy.Just ran into a problem while coding, search for days and I can't find the solution;(PLEASE HELP.



Allow me to explain:

Let's say I'v got a table , filled with lots of item name like "stone" "apple" , etc.
And another table filled with numbers(in my case they are turtle IDs).
And I want to match every item name with the number…

I kinda have an idea, that to make the third table, somehow use the item names as keys and get a number in.
Like Table.stone = num1, Table.apple = num2, etc.
But there're too many item names in the first table, and typing them in one by one would be too painful;)
I wonder is there a way to do it by code.



I checked into the Wiki and Youtube and Even the LUA site, seemed NO way out;( I don't even know if I can do that in Computercraft.
So, any idea guys?
p.s.I'm an Asian = Bad English. Thanks for your patience reading this:D
MKlegoman357 #2
Posted 02 January 2015 - 07:16 PM
I would like to see the definition of those two tables. For now, lets say one is called 'names' and the other is called 'numbers'. If those two tables are 'connected' like this: names[1] belongs to numbers[1], names[3] belongs to numbers[3] then it is quite simple to generate your third table:


local names = {"apple", "stone"}
local numbers = {2, 5}

local items = {}

for i = 1, #names do --// iterate through all the names
  items[ names[i] ] = numbers[i]
end

After that your third table is done! You can then use it like this:


print( items.stone ) --> 5

or save the table to a file so you could replace your two other tables:


local file = fs.open("items.txt", "w") --// open the file

file.write( textutils.serialize(items) ) --// write the serialized table into it

file.close() --// save the file
zilla_CN #3
Posted 02 January 2015 - 07:44 PM
I would like to see the definition of those two tables. For now, lets say one is called 'names' and the other is called 'numbers'. If those two tables are 'connected' like this: names[1] belongs to numbers[1], names[3] belongs to numbers[3] then it is quite simple to generate your third table:


local names = {"apple", "stone"}
local numbers = {2, 5}

local items = {}

for i = 1, #names do --// iterate through all the names
  items[ names[i] ] = numbers[i]
end

After that your third table is done! You can then use it like this:


print( items.stone ) --> 5

or save the table to a file so you could replace your two other tables:


local file = fs.open("items.txt", "w") --// open the file

file.write( textutils.serialize(items) ) --// write the serialized table into it

file.close() --// save the file

..Wow, that's..way much easier than I thought ;)/> Look like I made it too difficult for myself.
Just trying to use metatable or stuff. Kinda pain.
YOU DID SAVE THE DAY, my good sir;D
TheOddByte #4
Posted 02 January 2015 - 09:15 PM
I have a question, why do you even have it as two tables from the first time?

local items = {
	["apple"] = 2;
	["stone"] = 5;
}
This makes it so much easier, this is what MKlegoman's table will look like after it's done putting them together, so why not directly declare the table like this?
Edited on 02 January 2015 - 08:49 PM
Bomb Bloke #5
Posted 02 January 2015 - 10:04 PM
there're too many item names in the first table, and typing them in one by one would be too painful;)
TheOddByte #6
Posted 02 January 2015 - 10:15 PM
there're too many item names in the first table, and typing them in one by one would be too painful;)
Yeah yeah, I know.. I should read more carefully before posting :P/>
But it feels there's a way todo what he wants without using 2 tables, but it's kinda hard to know without any code.