Jump to content
  • 0

How to remove an element from table?


hoangninyb

Question

hi guys! I'm trying to find a method to remove an element from the table!

 

A ={"a","b","c"}

P = prompt({"input"},"{"a"})

for i, v in ipairs(A) do

    if P[1] == A[i] then

        A[i] = nil

        break

    end

end

 

when i set A[1] = "a" to A[1] = nil then print out nil, but i want print out "b"

 

 

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

function removeObject(targetTable,myIndex) 
    table.remove(targetTable,myIndex)
end

x = {"name","yame"}

removeObject(x,1)
print(x[1])    
 
--Example, Function removes a table item from passing on the target table and index this returns the text "yame" 
--Im expecting other users to improve or post their way of removing items from a table

Try this 😄 

Link to comment
Share on other sites

2 hours ago, Crystal_Mods100x said:
function removeObject(targetTable,myIndex) 
    table.remove(targetTable,myIndex)
end

x = {"name","yame"}

removeObject(x,1)
print(x[1])    
 
--Example, Function removes a table item from passing on the target table and index this returns the text "yame" 
--Im expecting other users to improve or post their way of removing items from a table

Try this 😄 

This is just recursion for nothing important xD, why don't you just use

table.remove()

Directly ?

Link to comment
Share on other sites

39 minutes ago, Crystal_Mods100x said:

both are the same functions and they do the same thing

The point was that you added redundant wrapper function instead of using "table.remove" directly. That's why an obvious question about the reason of doing it has followed.

Link to comment
Share on other sites

9 minutes ago, CmP said:

The point was that you added redundant wrapper function instead of using "table.remove" directly. That's why an obvious question about the reason of doing it has followed.

You don't get it do you? it still removes an item from the table??! i don't see whats wrong with using a function it but okay both do the same thing still 

both are functions and you have to enter both arguments 

Link to comment
Share on other sites

5 hours ago, hoangninyb said:

A ={"a","b","c"}

P = prompt({"input"},"{"a"})

for i, v in ipairs(A) do

    if P[1] == A[i] then

        A[i] = nil

        break

    end

end

Your codes are not wrong.(actually wrong in prompt P). cool. i'll use like this.

function arrangeTable(tab)
  local t = {}
  for i, v in pairs(tab) do --notice i use pairs instead of ipairs
    if v~=nil then table.insert(t, v) end
  end
  return t
end
A = {'a','b','c'}
P = gg.prompt({'put to remove'}, {'a'})
for i, v in ipairs(A) do
  if P[1]==v then A[i]=nil break end
end
print(A[1]) --output is nil
A = arrangeTable(A)
print(A[1]) --output is 'b'

 

If you confuse to use functions, you can also try with extra table or other methods. there are many ways to do this

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.