[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  table.remove stops for loop?
Home  |  Users  |  Search  |  FAQ
Register forum user name
Forgotten password?

table.remove stops for loop?

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Posted by Trevize   (21 posts)  [Biography] bio
Date Sun 10 Jul 2005 08:43 PM (UTC)
From what I've noticed it seems like when you use/call the routine (table.remove) in a table your looping it stops the loop?.. If thats the case it causes some problem for me.

Here's a test I did:

tableTest = {
{ name = "test1" },
{ name = "test2" },

function test()

for k, v in tableTest do
Note("k: " .. tostring(k) .. ". v.name: " .. tostring(v.name))
table.remove(tableTest, k)

for ke, ve in tableTest do
Note("ke: " .. tostring(ke) .. ". ve.name: " .. tostring(ve.name))


Frist time I run function test.
It shows:
k: 1. v.name: test1
ke: 1. ve.name: test2

See, at tableTest1, it aborts the loop after the first table.remove. And then the second for loop, it shows that the table now only has the second value.

The real function I actually noticed this in, is alot bigger. And it call's another function and from there I remove the value from a table that's looping in the first function, and I would like to be able to remove any amount from 0 to 1000

Is there anyway to bypass this or maybe make an alternative way of doing this? (and also, the reason I loop the table is becuase im checking it with another, so I "have" to loop the table.)
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #1 on Sun 10 Jul 2005 10:37 PM (UTC)
table.remove() shifts the array elements after the removed element down in the array. So if you remove element 1, element 2 becomes element 1. My hunch is that when you remove element 1, and then move to the next position, the iterator goes to position 2, which no longer exists in the array, and therefore stops as it detects end-of-array.

Chances are that if you were iterating over not an array but an associative table with named keys (as opposed to numbers), this would work.

In general - in most programming languages - it isn't necessarily safe to remove elements from a table as you iterate over it.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

[Go to top] top

Posted by Trevize   (21 posts)  [Biography] bio
Date Reply #2 on Sun 10 Jul 2005 11:02 PM (UTC)

Amended on Mon 11 Jul 2005 12:09 AM (UTC) by Trevize

Ahh, thanks. Did't think about that, it makes sense..
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum

[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]