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

Help with table switching

Started by MrPepsi, 15 July 2015 - 06:17 PM
MrPepsi #1
Posted 15 July 2015 - 08:17 PM
I've been working on a program for about a week now, it's kind if noobish. It is a program that allows a user to control a wireless mining turtle through a computer, but it has a built in GUI in the terminal. Since the GUI needs to update the players current position, where blocks are placed and which spaces have no blocks/spaces where blocks are available, I decided to use tables. This was because you can add/remove information from the tables.




So I have my tables labeled like this:

Row0 = { contents }
Row1 = { contents }
Row2 = { contents }
Row3 = { contents }
...
Row10 = { contents }

Now one of the functions I tried to test which gave me the error of: "Table expected, got string" was this.


function AAvail()
 local RTE = CZ - MinZ -- disabled
 local TTE = CY - MinY -- Table # to Edit
 local CTE = CX - MinX -- Column # To Edit
 table.remove(Row..TTE, CTE)
 table.insert(Row..TTE, CTE, Available)
end

So a bit more information, Row is a string I declared earlier because I got an error of the table to edit being nil, so String Row = "Row". String Available = "O"


When I print out Row..TTE I get Row5, which is one of the tables names. I know it is a string, but how can I make this work?
Grim Reaper #2
Posted 15 July 2015 - 08:56 PM
I'm not entirely sure that I understand what you're attempting, but I do know the source of your error.

table.remove is used as follows:

table.remove(_table, key)
and table.insert is similar:

table.insert(_table, key, element)

Now, the code

Row..TTE
creates a string, seeing as both 'Row' and 'TTE' are strings. When you use the concatenation operator,

string_1 .. string_2
you're combining two strings, providing an invalid input to the first argument of table.remove and table.insert.
Lyqyd #3
Posted 15 July 2015 - 09:04 PM
Change your table structure:


Rows = {
  { contents },
  { contents },
}

print(Rows[y][x])
MrPepsi #4
Posted 15 July 2015 - 09:34 PM
Change your table structure:


Rows = {
  { contents },
  { contents },
}

print(Rows[y][x])

I'm not trying to print the information, I'm trying to have it update the table based on what the user inputs, so if you were at X position of 10 and Y position of 5 it would edit the 5th table, 10th slot.

:3 How would I go about this.
Grim Reaper #5
Posted 15 July 2015 - 09:46 PM
Lyqyd's right. Just apply it to what you're doing here:


Rows[TTE][CTE] = "Available"

While this might be just my inability to understand what you're attempting, could you please elaborate on what exactly the function you've provided is doing as well as describe the purpose of the variables you're creating?
Edited on 15 July 2015 - 07:49 PM
MrPepsi #6
Posted 15 July 2015 - 11:33 PM
Lyqyd's right. Just apply it to what you're doing here:


Rows[TTE][CTE] = "Available"

While this might be just my inability to understand what you're attempting, could you please elaborate on what exactly the function you've provided is doing as well as describe the purpose of the variables you're creating?


So like I said I'm making basically a 3D printing turtle program, using the arrow keys to move space to go up, shift to go down etc. You control the turtle through a computer terminal and it has a GUI. The GUI updates to show where you are and what's around you, X being taken, O being Available spot and " " being empty/you dug a hole there. So the function I gave was a function to add an X to the table where you place the block. This way it updates around you and you can use checks to see if there is a block on the table. the CX is current X position, MinX obviously being the minimum X position. This would tell you the key slot to input the "X" or Taken variable and the CY being Current Y and MinY gives you the table you want to edit.

So like I stated, currently my tables are listed as Row#, Row# etc. increasing by 1 each time, so if my Current Y minus the MinY was 5 I would be selecting to edit the table Row5. and then I would edit the Key based off of the CX - MinX, so if the current X was 12 and the MinX was 1 I would need to edit key slot #11 of table Row5. I'll try what Lyqyd suggested with the clarification of your post. But this post was just to get back to you wanted me to explain/elaborate.
Grim Reaper #7
Posted 16 July 2015 - 12:15 AM
So like I stated, currently my tables are listed as Row#, Row# etc. increasing by 1 each time, so if my Current Y minus the MinY was 5 I would be selecting to edit the table Row5. and then I would edit the Key based off of the CX - MinX, so if the current X was 12 and the MinX was 1 I would need to edit key slot #11 of table Row5. I'll try what Lyqyd suggested with the clarification of your post. But this post was just to get back to you wanted me to explain/elaborate.

What you've done here makes things more difficult than they need to be. Lyqyd's suggestion makes it easy to index the proper row table, so you can make the updates you want with ease as opposed to writing a long if-else chain to select the proper table.
MrPepsi #8
Posted 16 July 2015 - 01:23 AM
So like I stated, currently my tables are listed as Row#, Row# etc. increasing by 1 each time, so if my Current Y minus the MinY was 5 I would be selecting to edit the table Row5. and then I would edit the Key based off of the CX - MinX, so if the current X was 12 and the MinX was 1 I would need to edit key slot #11 of table Row5. I'll try what Lyqyd suggested with the clarification of your post. But this post was just to get back to you wanted me to explain/elaborate.

What you've done here makes things more difficult than they need to be. Lyqyd's suggestion makes it easy to index the proper row table, so you can make the updates you want with ease as opposed to writing a long if-else chain to select the proper table.

Alrighty, I'll give it a try, thank you both for the help!
jerimo #9
Posted 16 July 2015 - 07:11 PM
So like I stated, currently my tables are listed as Row#, Row# etc. increasing by 1 each time, so if my Current Y minus the MinY was 5 I would be selecting to edit the table Row5. and then I would edit the Key based off of the CX - MinX, so if the current X was 12 and the MinX was 1 I would need to edit key slot #11 of table Row5. I'll try what Lyqyd suggested with the clarification of your post. But this post was just to get back to you wanted me to explain/elaborate.

What you've done here makes things more difficult than they need to be. Lyqyd's suggestion makes it easy to index the proper row table, so you can make the updates you want with ease as opposed to writing a long if-else chain to select the proper table.

Alrighty, I'll give it a try, thank you both for the help!
Not saying this is the good way of structuring your program but having run into an occasion where this method was the only way you can use
_G[string name] to get to a table by string name in the global table
Best of luck in your program though