swarm-support
[Top][All Lists]
Advanced

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

removing elements while looping with an index, and return values from Se


From: Ken Gosier
Subject: removing elements while looping with an index, and return values from Set add
Date: Tue, 19 Mar 2002 07:08:55 -0500 (EST)

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];

So basically, b/c I'm removing some of the elements of oldList while I'm
traversing it with the index, I wind up with seg faults.
- One discussion in the archives suggests it'd be best to just save the
offsets of those elements that satisfy the condition, then remove them
manually in a separate loop.
- Also, I searched through Paul Johnson's FAQ, and found an item for
working with Lists, but not the answer to this specific question.

Many thanks for any help or pointers. It seems like this is a
commonly-faced problem.


2nd question: I'm adding elements into a set with an error check like so:

if (  ! [mySet add: thisElem]  )
{
   fprintf(stderr, "Failed to add element\n");
}

This always prints out the error message. But then when I loop over the
set to print out the elements, it's always been added. The ref guide says
a BOOL is passed back from add. Help in figuring out this behavior?


Many thanks for any help,

-- 
Ken Gosier
address@hidden


                  ==================================
   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]