hoangninyb Posted November 8, 2021 Posted November 8, 2021 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"
Crystal_Mods100x Posted November 8, 2021 Posted November 8, 2021 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
MAARS Posted November 8, 2021 Posted November 8, 2021 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 ?
Crystal_Mods100x Posted November 8, 2021 Posted November 8, 2021 5 minutes ago, MAARS said: This is just recursion for nothing important xD, why don't you just use table.remove() Directly ? both are the same functions and they do the same thing
CmP Posted November 8, 2021 Posted November 8, 2021 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.
Crystal_Mods100x Posted November 8, 2021 Posted November 8, 2021 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
Lover1500 Posted November 8, 2021 Posted November 8, 2021 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
Question
hoangninyb
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"
6 answers to this question
Recommended Posts
Archived
This topic is now archived and is closed to further replies.