Reputation: 3828
I want to write a function where a list containing a set of agents are removed if a certain condition is satisfied. In this situation, if the turtle-own variable is set deceased? = true, remove them from the list. I have a bunch of lists and want to do that for all the lists, hence I am writing a function to do that. I am not sure how to write report within the function or whether even if it's necessary. Also how do I call the function
breed [traps trap]
breed [Cages cage]
breed [foxes fox]
traps-own[list-trapped-foxes]
Cages-own[list-caged-foxes]
foxes-own [deceased?]
to-report remove-agents [list-x]
foreach list-x[
i ->
ask i[
if deceased? = True [
let agent-to-be-removed self
ask myself [
set list-x remove agent-to-be-removed list-x
report list-x
]
]
]
]
end
to renew-agents
ask traps [remove-agents list-trapped-foxes]
ask Cages [remove-agents list-caged-foxes]
end
Upvotes: 0
Views: 1129
Reputation: 1736
a) It is rarely advisable (or even allowed) to modify a list while you are iterating through it, as you are trying to do here.
b) As JenB said, it would be wiser to use agentsets instead of lists if you can. (One reason you might need lists is if the same agent needs to be on the list more than once; preventing that might be a reason to use agentsets.
If list-x was an agentset, you would use: set list-x list-x with [not deceased?]
c) What you need for lists is the primitive "filter": I did not try it but this should work instead of your whole reporter: set list-x filter (not deceased?) list-x
(Remember always to see whether what you need has already been written before starting again, especially for a task as common as this!)
Upvotes: 1