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

Tables Confusing me

Started by Czarified, 20 May 2013 - 07:02 PM
Czarified #1
Posted 20 May 2013 - 09:02 PM
Hi, so recently I posted a thread for the custom map I'm trying to make. There didn't seem to be much interest in the matter, but I still need help on it and did not want to triple post in a dead-ish thread.

I have several different locations that are randomized and put into a table. For each location I need an "incoming" , "outgoing" , and "available" table that I can access after the locations have been randomized.

Example:

Locations = { "Stop1", "Stop2", "Stop3", "Stop4", "Stop5" }
-- Randomize but only select a given number. Let's say only choose 3 different options.
Table = { "Stop2", "Stop1", "Stop3" }

Now I want to access the different corresponding tables for the randomized values. I need to move random things from the "available" table for Stop2 to the "outgoing" table for Stop2 and the "incoming" table for "Stop1."

Is there an easy way to do this and what's the best structure/method for doing so? I've been trying for a while to figure it out on my own and from help I received last week in my project thread (Located here). I just can't get it and I'm kind of discouraged at this point… I apologize for any inconvenience, I'm only a newbie programmer and this is the biggest project I've taken on so far, probably too big for my skill level… :unsure:/> If anyone likes the idea, I'm definitely okay with including you in the project and giving credit where credit is due! I could really use some help on this one.
Bubba #2
Posted 20 May 2013 - 09:29 PM
You could use tables within tables - that is very convenient for values that require multiple variables to be set.

But it seems like in this case you may just want to use the locations as indexes and the status (incoming, outgoing, or available) as the value. Here's an example:

local myTable = {["Stop1"] = "incoming", ["Stop2"] = "outgoing", ["Stop3"] = "available"}

print(myTable["Stop1"]) --Prints "incoming"
myTable["Stop1"] = "available"

One thing you need to note is that you should never use the keyword "table" as a variable name because you will be overwriting the functionality of some important functions such as table.insert(). In your case it's fine because you used the keyword "Table" (with a capital), but just keep that in mind.
Czarified #3
Posted 20 May 2013 - 09:38 PM
Thank you bubba. I've thought about using the locations as indices but just couldn't figure it out. How would I store data in the incoming, outgoing, and available tables, using this format? remember I need a way to access them after I've randomized the locations and stored in another temporary table.

also thanks for the tip about table names!
diegodan1893 #4
Posted 21 May 2013 - 01:26 PM
Also, if you want you can do this:

local myTable = {Stop1 = "incoming", Stop2 = "outgoing", Stop3 = "available"}
as in a table in Lua, ["Stop1"] and Stop1 is the same.

This also works when printing but you need to write a dot as if it were an API (in fact, all functions and variables in Lua are stored in tables):

print(myTable.Stop1)
myTable.Stop1 = "available"
Czarified #5
Posted 21 May 2013 - 04:32 PM
Also, if you want you can do this:

local myTable = {Stop1 = "incoming", Stop2 = "outgoing", Stop3 = "available"}
as in a table in Lua, ["Stop1"] and Stop1 is the same.

This also works when printing but you need to write a dot as if it were an API (in fact, all functions and variables in Lua are stored in tables):

print(myTable.Stop1)
myTable.Stop1 = "available"

so Stop1 is a table that contains data and is the index for the string value "incoming?"
diegodan1893 #6
Posted 21 May 2013 - 05:08 PM
so Stop1 is a table that contains data and is the index for the string value "incoming?"

myTable is a table that contains data. Stop1 is the index for the string value "incoming". Think on Stop1 as the name of the variable: you can do something = "hello" outside a table, something is the name and "hello" the value. Table indexes are the same but inside a table.

myTable = {"hello", "world"}
print(myTable[1]) --prints "hello"
print(myTable[2]) --prints "world"

myTable = {["index"]="value"}
print(myTable["index"]) --prints "value"

--But if you don''t want to write [" and "] everytime you can do
print(myTable.index) --prints "value"

When I said that all functions and variables are stored in tables I was talking about how lua manages variables, but Stop1 is not a table in this case (but it can be a table, as you can set any tipe of variable as a index, even a function).