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

How to remove data from table?

Started by MrScissors, 25 October 2015 - 06:11 AM
MrScissors #1
Posted 25 October 2015 - 07:11 AM
So basically i want to have program, that displays all content from root ("/") folder on computer. But i want it to not display rom and startup. I got stuck at this code. What i want to do, is for loop.

for i = 1, #list do
<code>
end
In this loop i want to check if next and next value in list table
(accesed this way):

local list = fs.list("/")
Is the same as "rom" or "startup"

if list[i] == "startup" or list[i] == "rom" then
<CODE>
end
I want to remove this value from table. Can anyone help me with it?
Creator #2
Posted 25 October 2015 - 09:29 AM
Yep, here you go:

local t=files.list("/")
for i=1,#t do
    if t[i] == "rom" or t[i] == "startup" then
	    t[i] = nil
  end
end
for i,v in pairs(t) do
   print(v)
end
Bomb Bloke #3
Posted 25 October 2015 - 10:45 AM
Or, if you don't want gaps in the final table:

local t = files.list("/")

for i = #t, 1, -1 do  -- Count down from the end of the table to the start.
  if t[i] == "rom" or t[i] == "startup" then
    table.remove(t, i)  -- Removes index i and moves all higher indexes down to fill the gap.
  end
end

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