swarm-support
[Top][All Lists]
Advanced

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

Re: Problems with lists


From: Benedikt Stefansson
Subject: Re: Problems with lists
Date: Fri, 05 Jun 1998 14:05:33 +0200

Alessandro Perrone wrote:

> I have a little problem with Lists.
> All I'd like to do is to cancel an object from List.
> Here it is the methos I wrote
>
> -destroyInefficient {
>   id index, agent;
>   int i;
>   index = [[modelswarm getFirmList] begin: [self getZone]] ;
>   while ((agent = [index next])) {
>   if (status !=1) {
>     [[modelswarm getFirmList] remove: agent];
>     [index remove];
>     [agent drop];
>                     }
>          }// while
> [index drop];
>   return self;
> }
>
> Whats wrong?

1) You can't remove the agent from the list while in the loop - the index
doesn't like that 2) The error message stems from the fact that you already
removed the agent from the list and now the index can't remove him because
it's screwed up.

Doug Donaldsons explained a solution to this problem on May 20th. I haven't
tried it myself.

There may be a simpler solution in this case. It seems from your example that
you are simply dropping all agents from the list, in which case you could
say:
    [[modelSwarm getFirmList] forEach:M(drop)];
    [[modelSwarm getFirmList] removeAll];
Or, if you need to drop a subset of agents, then say:
      firmList=[modelSwarm getFirmList];

     index = [firmList begin: [self getZone]] ;
      reaperQueue=[List create: [self getZone]];

      while ((agent = [index next]))
       {
           status=[agent getStatus]; <- Or whatever
           if (status !=1)
                [reaperQueue addLast: agent];
        }
       [index drop];

       index=[reaperQueue begin: [self getZone]];

      while((agent=[index next]))
            [firmList remove: agent];
        [index drop];

        [reaperQueue forEach: M(drop)];
        [reaperQueue drop];
This way you have completely disentagled the operation of iterating over the
list and removing select members of the list.

--
With best regards,
-Benedikt

-------------------------------------------------------------------
Department of Economics, UCLA, Los Angeles, CA 90095-1477, USA
Tel: (310) 825 4126     Fax: (310) 825 9528

4/3-7/1 1998:
Dipartimento di Economia, Universita di Trento, 38100 Trento, ITALY
Tel: +39 (461) 88 22 46 Fax: +39 (461) 88 22 22




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