April 28, 2024, 11:09:00 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


for each breaks with listremove?

Started by Todd Riggins, July 18, 2007, 03:41:30 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Todd Riggins

Is that right? Below goes through the list with the for/each. If it finds the node that has the pointer to 7, it will then list remove the node. So then the for/each loop will break and not continue right? How would I make it continue? If the list is removing a node then the 'prev node' and the 'next node' should connect... shouldn't it? Thus, the for/each loop should still be able to continue.

thx

' compile in console mode

mylist = ListCreate()

'add some items to the list
FOR x = 0 to 10
temp = ListAdd(mylist,NEW(INT,1))
#<INT>temp = x
NEXT x

'iterate through the list
pos = ListGetFirst(mylist)
WHILE pos <> 0
pData = ListGetData(pos)
PRINT #<INT>pData,
pos = ListGetNext(pos)
ENDWHILE
PRINT

For m=Each mylist as INT
print #<INT>m
UpdateMyList( m,7 )
Next
print
For m=Each mylist as INT
print #<INT>m
Next

'show the list sans the removed item
pos = ListGetFirst(mylist)
WHILE pos <> 0
pData = ListGetData(pos)
PRINT #<INT>pData,
pos = ListGetNext(pos)
ENDWHILE
PRINT
'clear out the entire list
ListRemoveAll(mylist,TRUE)

PRINT "Press any key to close"
DO:until inkey$ <> ""

end

sub UpdateMyList( pointer ml, int num )

if #<INT>ml = num
'remove a data item
pos = ListGetFirst(mylist)
WHILE pos <> 0
pData = ListGetData(pos)
IF #<INT>pData = num
pos = ListRemove(pos,TRUE)
ELSE
pos = ListGetNext(pos)
ENDIF
ENDWHILE
endif
endsub
Brought to you buy: http://www.exodev.com

Ionic Wind Support Team

You're running into a common problem with linked lists.  FOR EACH has an internal 'pos' pointer that won't get updated when you are removing items in a different loop.  Since you are stopping at the node that you want to remove the 'next' pointer of that node gets nulled by your ListRemove and that FOR EACH drops out.

For a correct way to do it see the users guide under "using linked lists" and specifically "Removing elements while iterating"

FOR EACH is a convenient way for reading a list, but it is useless for deleting items.

Paul.

Ionic Wind Support Team

Todd Riggins

Quote from: Paul Turley on July 18, 2007, 04:20:47 PM
For a correct way to do it see the users guide under "using linked lists" and specifically "Removing elements while iterating"
Exactly! Thats why I got confused when that part of the docs says:

ListRemove returns a pointer to the next node in the list after the deleted node or NULL if the end of the list has been reached.  It is important to remember that ListRemove takes the place of ListGetNext in this instance.

but ok then... thx

Brought to you buy: http://www.exodev.com

Ionic Wind Support Team

Yes but you were using one FOR EACH loop to try and find a node and a second loop to try and remove it.  You removed a node without the first loop knowing any better.

This:


For m=Each mylist as INT
print #<INT>m
   UpdateMyList( m,7 )
Next

Isn't really needed as just a single call to UpdateMyList, without a pointer, would work.  Guess I just am not seeing the reason for the initial FOR EACH loop when UpdateMyList itereates through the entire list anyway. 

Paul.
Ionic Wind Support Team

Todd Riggins

I understand what you are saying...

The example I have stems off from porting blitz3d code over to ebasic. blitz3d has something similuar with structures types and using for/each loop. The first loop i have would go through each node and update the type. Like for a bullet, update it's position, check collision. The second loop with the listremove is when for example with the bullet, if a collision occurs on the bullet, then i want to remove it from the list.

I was trying to keep the code as close as possible, but I will do it different now. thx
Brought to you buy: http://www.exodev.com

Ionic Wind Support Team

Perhaps the 'frags2d' example might be of some use to you which continually updates a list of objects as they expire, removing and adding items to the list.
Ionic Wind Support Team