'counting how many times an agent appears in a list in netlogo ("expected a literal value" error)
I'm trying to count the number of times an agent appears in a list in netlogo. I have a list of agents (it's an output from running draws of a multinomial distribution) and I want to save the number of times an agent appears in the output list. The output is not an agentset given that agents can appear multiple times within a list. The main error that I am running into is that when I try to filter the list, it says "expected a literal value."
Note: output is the name of the list and it is saved as a patch variable. For example, "show output" gives the following: (patch 0 0): [(turtle 44) (turtle 44) (turtle 6) (turtle 19) (turtle 33) (turtle 33)] Not every turtle appears in the list but some turtles appear multiple times.
First, I tried the below code to get turtles to filter the output list to exclude non-self:
ask turtles-here [
show filter [i -> [i != who of myself]] [output]
let output-me-only show filter [i -> [i != who of myself]] [output] ;to create subsetted list
set number-me-appears length [output-me-only] ;to save number of times the turtle appears in the original list ("output")
show number-me-appears
]
Because that didn't work, as it gave the "expected a literal value" error, I tried the below code:
ask turtles-here [
show filter [i -> [i != self]] [output]
]
Both attempts resulted in the "expected a literal value" error.
I thought the "literal value" error may be coming up because the list is made up of turtle IDs that are not simple numbers, but are also not recognized as agents given that it is not technically an agentset. I tried converting the list to literal ID numbers using the "who" primitive, but was not successful. See below:
let converted-output []
show map [ x -> who] [output]
let converted-new-infections insert-item x
This did not work because of the "expected a literal value" again.
Thank you for your help on this!
Solution 1:[1]
The solution quite simple: you remove the brackets from [output]. Output is already a list so you are turning the input for your anonymous procedure into a list containing a list. As such, the anonymous procedure tries to use the entire output list as value for i.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | LeirsW |
