Subject: RE : loop and ID
From: Christophe Le Page (christophe.le_page@cirad.fr)
Date: Thu May 06 2004 - 12:44:07 CEST
-----Message d'origine-----
De : owner-cormas@cirad.fr [mailto:owner-cormas@cirad.fr] De la part de
Pierre-Marcel GORAL
Envoyé : mercredi 5 mai 2004 11:55
À : cormas@cirad.fr
Objet : loop and ID
Hello everybody!
I try to use functions in an economical model, in order to change the
configuration of the economical environment, to observe the behaviours
of my agents.
To do this I need to implement a certain amount of functionnalities
using the ID of the agents. Is it possible to read those IDs, and with
what kind of methods?
[clp] yes, very easily, with the read-access method #id that exists for
any kind of cormas entities. When a new instance of any subclass of
Entity is created, Cormas automatically assign an integer value to its
"id" attribute.
I've got another question: it seems that in Cormas the loop of the
"for(i=0;i<...;i++)" kind is written like:
x to: y do: [:a | something].
That's OK, but how do I use the index variable in the block executed
during the loop???
[clp] Do not search for complexity !!! In smalltalk iterations are more
easy to handle than in C++ or in Java !!! In you example, the index
variable is a !!!
I need this in order to compare it with ID, joining the first
problem... You know, just like this in pseudo C:
for(i=0;i<...;i++){
(anAttrib of the agent whose ID == i) = 1;
}
[clp] to do something of the 10 first agents of your population, write
something like:
|candidate|
1 to: 10 do: [:i |
candidate := self theAgents detect: [:anAgent | anAgent id = i]
ifNone: [nil].
(candidate notNil and: [candidate age = 18]) ifTrue: [candidate
vote]]
Thanks for all!