[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: |
Ken Gosier |
Subject: |
Re: removing elements while looping with an index, and return values from Set add |
Date: |
Wed, 20 Mar 2002 07:25:37 -0500 (EST) |
Hello all again. Many thanks for all the replies. I am now using index
remove and not getting the seg faults.
However, I am now having a new problem. I am able to resolve it myself,
but not happy with my solution. Specifically, the removing of elements I
posted yesterday actually takes place in a 2-step loop:
for (i=0; i<2; i++)
{
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[i] )
{
[newList addLast: [index remove]];
}
}
[index drop];
... do stuff with newList ...
}
I first want to get those elements that satisfy condition[0], then those
that satisfy condition[1]. I find that everything goes smashing, wonderful
in the first pass through. But then the second time through, the index
steps off the end of the List, and I get seg faults, b/c messages get sent
to nil id's.
So I re-wrote the code block as:
for (i=0; i<2; i++)
{
id <List> newList = [List create: self];
id <Index> index = [oldList begin: self];
id thisElem;
fprintf(stderr, "Number of elems in oldList: %d\n",
[oldList getCount]);
for (thisElem=[index next]; thisElem != nil;
thisElem=[index next])
{
if ( condition[i] )
{
[newList addLast: [index remove]];
}
}
[index drop];
... do stuff with newList ...
}
So I've made 2 changes:
- added in the fprintf(stderr,...) to show me the size of oldList.
- changed the inner for condition from ([index getLoc]==Member) to
(thisElem != nil).
I find that the fprintf statement shows me that oldList has been shortened
after the first pass. And with the second change, I no longer get seg
faults, and the sim runs well.
But like I said, this solution works, but I'm not really happy with it.
Why should the index step off the end of the list after the first
pass-through?
Many thanks again 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.