After a number of times when I had to iterate arrays, mark some elements for removal, and then remove them in a second loop, I wished I'd be able to do it in one go. Doing it "by hand" with a while loop managing the index variable explicitly sounded cumbersome, so it occured to me I could write a custom iterator instead. Hello Hisham and all, this is my first post to this great list. Removing elements from array is classic problem that can be solved easily with backwards loop: print("** Remove backwards: **") local t = { "a", "b", "c", "d", "e" } for i=#t,1,-1 do local v = t[i] print(i, v) if v == "c" then table.remove(t, i) end end Looping is extremely fast, moving items in memory is very slow. By looping backwards we have least amount of items to move. Think of situation where you want to remove { "a", "c", "d", "e" } from t, in this case there will be only one memory move: "b" from index 2 to index 1. Basically when you optimize memory allocations and memory moves you have optimized your code well - in any language. Regards, Pasi Mankinen Finland
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル