Query about DELETE

Previous topic - Next topic

MrTAToad

With the following :

Code (glbasic) Select
DEBUG "Looking for message : "+rMessageID%+"\n"
FOREACH message IN messageList[]
DEBUG "Message ID : "+message.messageID%+" "+rMessageID%+"\n"
IF message.messageID%=rMessageID%
DEBUG "Deleted message 1 : "+rMessageID%+"\n"
DELETE message
DEBUG "Deleted message 2 : "+rMessageID%+"\n"
RETURN TRUE
ENDIF
NEXT

DEBUG "Message not found : "+rMessageID%+"\n"
RETURN FALSE


I find the following debug messages are displayed :

QuoteLooking for message : 21
Message ID : 21 21
Deleted message 1 : 21
Message not found : 21

Am I right in assuming that DELETE will force the message loop to continue onto the next array item or does it immediately break out of the loop ?

The important thing is that anything after DELETE wont be executed.

MrTAToad

#1
Indeed - I dont see why code after DELETE  shouldn't execute - unless its protection against trying to use the type when it would no longer exist.

Apparently, it does a CONTINUE afterwards...  So its not a bug :)

Moru

Nothing after a DELETE gets executed, it directly starts over with the loop.

MrTAToad

Yes - comes of not looking the details for the command :)

Kitty Hello

About the inside:
Code (glbasic) Select

FOREACH a in arr[]
  PRINT a, 0,0
  DELETE a
NEXT


Will produce:
Code (glbasic) Select

for(int foreacha=0; foreacha<BOUNDS(arr,0);  ) // make a loop for each element in arr[]
{
   DGInt& a=arr(foreacha++); // Get a reference (pointer that is) to arr[foreacha]
   PRINT( a, 0,0);
   DIMDEL(arr, --foreacha); continue; // the delete removes the element at that position, continuing at the position after that element
}


So, as you can see "a" is only a pointer and after a DELETE it points to nothing (or is broken). Thus the "continue".