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

Gammon Software Solutions 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
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: table.remove stops for loop?
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please).
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

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

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

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

Posted by David Haley   USA  (3,881 posts)  [Biography] bio   Moderator
Date Sun 10 Jul 2005 10:37 PM (UTC)  quote  ]
Message
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

http://david.the-haleys.org
[Go to top] top

Posted by Trevize   (21 posts)  [Biography] bio
Date Sun 10 Jul 2005 08:43 PM (UTC)  quote  ]
Message
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()

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

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

end

Frist time I run function test.
It shows:
tableTest1:
k: 1. v.name: test1
tableTest2:
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

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.


3,219 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

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

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]