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

Table.remove Isn't Working.

Started by luza, 21 February 2012 - 12:09 PM
luza #1
Posted 21 February 2012 - 01:09 PM
table = {"lukas","peter","hans"}

for i=1, #table, 1 do
    print(table[i])
end

table.remove(table, 2)


for i=1, #table, 1 do
    print(table[i])
end

I get the output:

lukas
peter
hans
attemt to call nil

Can't I use the table.remove function in CC?
Casper7526 #2
Posted 21 February 2012 - 01:12 PM
don't name your table - table, table is the name of a function
Advert #3
Posted 21 February 2012 - 01:13 PM
Edit: Derp, read casper's post :)/>/>


It works for me; are you sure you haven't removed the table.remove function?
luza #4
Posted 21 February 2012 - 01:18 PM
well, that was just an example code. this is my actual code:


function deleteLine(table, index) --expects string, number
	local path = getTablePath(table)
	local fields = {}
	fields = getFields(table)

	local file = nil
	local field_content = {}
	local file_path = nil
	for i=1, #fields, 1 do


		field_content = getField(table, fields[i])

	    -- everything is working until here.

		table.remove(field_content,index)

		file_path = path..fields[i]
		file = io.open(file_path, "w")
		file:write(field_content)
		file:close()
	end
end
Casper7526 #5
Posted 21 February 2012 - 01:23 PM
hmmm not positive… cause table.remove works for me…
luza #6
Posted 21 February 2012 - 01:27 PM
I just tested table.remove() with my example code. works if I don't use table as a variable name. So the error must be in my function.. :/
Espen #7
Posted 21 February 2012 - 04:19 PM
'table' = global table with the name 'table'.
'table' = your function argument for deleteLine() called 'table'.

The argument of deleteLine() which receives a table is named 'table' and is local to this function.
That means while you're within your function, table.remove() will try to call the remove() function from 'table' and not from the global 'table'.

Remember that calling table.remove() is the same as retrieving from the table 'table' the value for the key "remove", which is then executed as a function:
table.remove() == table["remove"]()

So within your function you call table.remove(), but the table you receive as a function argument doesn't hold the remove() function, because it is whatever table you fed deleteLine() and that most probably won't coincidentally contain the remove() function of the global table, unless you specifically inserted that into it. :huh:/>/>
If you want to call the remove() function from the global 'table', then you either have to use another argument name than 'table', or you have to copy the global 'table' into another variable before you call your function deleteLine().
For example:

function deleteLine( table, index )
  -- some other ode
  gTable.remove( table, 2 )
end

local gTable = table
local customTable = { "abc", "def", "xyz" }
deleteLine( customTable, 2 )
Disclaimer: Haven't tested the code yet.

EDIT: Corrected an error in the code.
Edited on 21 February 2012 - 03:26 PM
luza #8
Posted 21 February 2012 - 04:37 PM
aaaaaaaaaaaah thanks.

:huh:/>/>