swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: removing elements while looping with an index, and return values fro


From: Paul Johnson
Subject: Re: removing elements while looping with an index, and return values from Set add
Date: Tue, 19 Mar 2002 08:06:58 -0600
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.8) Gecko/20020212

Removing an item with [oldList remove: thisElem] breaks the index that is based on the list. For cases like yours, you should use the remove method of the index itself, because the index is aware of the change.
[newList addLast: [index remove]];
No matter what we had in the Swarm User Guide, your for() loop is the recomended way to traverse a collection because it does not stop on nils.

Ken Gosier wrote:
2 questions about a simple sim I'm coding up:

I'm sure the first question is an RTFM question, so if I can get a pointer
to the proper FM to R, I'd be most grateful. :) Anyway, I'm using an index
to loop over a List, and I want to remove elements which test positive for
a certain condition:

id <List> newList = [List create: self];
id <Index> index = [oldList begin: self];
id thisElem;

for (thisElem=[index next]; [index getLoc]==Member; thisElem=[index next])
{
   if (  condition  )
   {
      [newList addLast: [oldList remove: thisElem]];
   }
}
[index drop];




--
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ku.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700


                 ==================================
  Swarm-Support is for discussion of the technical details of the day
  to day usage of Swarm.  For list administration needs (esp.
  [un]subscribing), please send a message to <address@hidden>
  with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]