I'd like to subset agents based on values I have in a list. In the code below I have a list of values, called "ActiveID". I'd like to create an agentset of all the turtles who have a turtle-own "ID" values that matches with the numbers in the list "ActiveID". I'd like the turtles with matching values to be not hidden, while all those with "ID" values not in the list to be hidden.
Eventually, each tick the list will change.
The code below doesn't work because I loop through the list "ActiveID" one at a time, resulting in turtles momentarily becoming visible, but then becoming hidden again as the code continues to move through the list. I'd ideally like the turtles with "ID" values that match those in the list to stay not hidden.
globals [ ActiveID ]
turtles-own[ ID]
to setup
ca
create-agents
create-list
reset-ticks
end
to go
Activate
tick
end
to create-agents
create-turtles 20 [
set ID random 10
set size 4
set color red
setxy random-xcor random-ycor
]
end
to create-list
set ActiveID [ 0 4 9]
end
to Activate
foreach ActiveID [ ?1 ->
ask turtles [
ifelse ID = ActiveID [
set hidden? false
] [hide-turtle]]]
end ```
Doing things in the right order definitely makes a difference. It’s also good to think about who is doing what to whom.
Like, is the list making the turtles do something? … or are the turtles reading the list and doing something?
And if the list was in control… how could they talk to the turtles to make sure they only changed once?
You could hide all the turtles, then use the list to show the ones that need showing.
But that means talking to the turtles once for every item in the list, and once more!
There must be a better way!
You can use the POSITION
reporter.
Position searches a list for a value, and reports the index of the value, or FALSE
if the value is not in the list.
So you’d do something like this:
Ask turtles
[ set hidden?
((position ID ActiveID) = false)
]
You can make an agent set, using WITH
:
Let Active-Agents turtles with
[ position ID ActiveID != false ]