Page 1 of 1

safe to removeitem from emptytable?

Posted: Sat May 27, 2023 12:38 pm
by Bugala
Since manual doesnt mention how RemoveItem() command reacts to empty table, I am checking this to make sure it is safe to do so.

When I do:

Code: Select all

mytable={}

InsertItem(mytable, 2)

item1 = RemoveItem(mytable)
item2 = RemoveItem(mytable)
item3 = RemoveItem(mytable)

DebugPrint(item1)
DebugPrint(item2)
DebugPrint(item3)
It simply returns NIL on item2 and item3 cases, which are RemoveItem() on an empty table, meaning I could keep removing stuff from the table and keep waiting for it to return NIL to see that all the items from it have been removed.

But since this is not commented on RemoveItem() documentation, is this actually a safe way to do it?

Re: safe to removeitem from emptytable?

Posted: Sat May 27, 2023 2:53 pm
by emeck
@Bugala

Why don't you check with IsTableEmpty() before removing?

Re: safe to removeitem from emptytable?

Posted: Sat May 27, 2023 7:31 pm
by Bugala
@emeck

Thanks from the tip, didnt realise there was specific command for this. This will probably do the trick for didnt want to use TableItems to do the check since I was expecting it could be slow and I was in need of speed. but I suppose checking whether removed item is NIL or IsTableEmpty(), chances are IsTableEmpty() is even faster, since I dont then need to do the RemoveItem() part at all in case it is empty.