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

table help

Started by menz, 22 November 2012 - 02:48 PM
menz #1
Posted 22 November 2012 - 03:48 PM
ive been struggling with a way to remove specific lines from a table, and keep the rest of the data intact.

here is basically what the purpose is…

im encoding all rednet messages into a table for later parsing, once the computer has finished its current program it will parse the table for any lines that apply to it and removing those that dont then execute that line. once finished it begins parcing the data again.
OmegaVest #2
Posted 22 November 2012 - 03:51 PM
Hmm. If you know what line are not meant to be implemented, then just shunt all other lines to a separate table.

psuedo:

tabel = {}
tableture = 1
for i =1, #table do
   if tabel[i] == relevant then
	  tabel[tableture] = table[i]
	  tableture = tableture +1
   end
end
table = {}
table = tabel

Then carry on.
Kingdaro #3
Posted 22 November 2012 - 04:04 PM
I'm not sure I understand your situation fully, but you can use table.remove() to remove elements from a table. It is used in the following manner:

local someTable = {'foo','bar','foobar'}

table.remove(someTable, 2)
This code removes the second element, "bar", from "someTable".
menz #4
Posted 22 November 2012 - 05:13 PM
king: table.remove returns errors, perhaps im not using it right.
omega: ive thought about doing it that way however im not sure if its the right way to go about it.

ok, i realize i wasnt specific enough, so heres a shot at being more specific. i have a rednet program running in parrellel with my main program that saves all incoming messages into this format

table = {
[1] = {id = 21, msg = "help"},
[2] = {id = 43, msg = "msg 2"},
[3] = {id = 27, msg = "and so on"}
}

later on when the secondary code is ready to run, i am parsing that table for relevent info. what i want to do is remove the first nested table after i have reviewed it…

im wondering if there was a simple way using only one table.

for a test im using this script as the actual program isnt fully written yet ;)/>/>

for i = 1, #table do
print (table[i].msg)
end
Luanub #5
Posted 22 November 2012 - 05:22 PM
Is your table named table or something else? If it is table you're overwritting the table API, thus why table.remove() errors.
menz #6
Posted 22 November 2012 - 05:29 PM
OMG - facepalm
lol in testing yes i was using table as a table name…
so yes table.remove is what i was looking for worked perfectly after i changed the name.

thanks all.