ask patches and build a list from their variables

24 views
Skip to first unread message

Sarah Smet

unread,
Jun 21, 2022, 12:50:16 PM6/21/22
to netlogo-users
Hello,
I don't succeed in building a list from patch variables, see a reproducible example

globals [ABC LU IDlist]
patches-own [seed ID AT]
  set ABC [ "A" "B" "C" "D" "E" "F" "G" "H" "I" ]
  set LU ["apple" "pear" "citrus" "banana" "orange"]
  ask patches [ set seed random 10 set ID one-of ABC set AT3 one-of LU
    ifelse (ID = "A")
    [ set pcolor orange] [set pcolor white]
  ]

set AT-data [["A" 65 81] ["B" 21 71] ["C" 54 18] ["D" 23 41] ["E" 85 27] ["F" 35 88] ["G" 29 4] ["H" 78 2] ["I" 99 60]]

ask patches [
    let i 0
      while [i < length AT-data] [
      let current-inner-list item i AT-data
     set IDlist [ ]
      ifelse (ID = item 0 current-inner-list) [
       set IDlist lput AT3 IDlist ;; this not working as expected
      print IDlist
       stop
      ]
      [ set i i + 1]
    ]
  ]

The idea is to generate a IDlist for each item of AT-data where it is filled with the AT3 variable of all patches that share the same ID (as per AT-data list).
The idea is to create a temporary list, check something (not written here) and then move on to the next item of AT-data
That is why I "reinitiate" the IDlist.

The results is just a IDlist of length one, which should not be, as multiple patches share the same ID.

I have tried many things, including using foreach, or creating an initial list with the ID and then replace-item i + lput commands i for each ID.... but it looks like I'm missing something.

Many thanks, best
Sarah

Charles Staelin

unread,
Jun 21, 2022, 4:10:39 PM6/21/22
to netlogo-users
Sarah,

I'm not exactly sure what you are after, but does the following give you what you want?

  foreach At-data [ a ->
    let ap patches with [ID = item 0 a]
    set IDlist remove-duplicates [AT3] of ap
    show IDlist
  ]

This finds all patches that share the given ID, and then makes a list of all the values of AT3 that those patches have.

Charles

Sarah Smet

unread,
Jun 22, 2022, 1:06:10 PM6/22/22
to netlogo-users
Dear Charles,

That is exactly what I was looking for, it does the job perfectly (and remove-duplicates in the same time).
That is a bit of learning again, It is great to see how expert are helping out juniors, I hope to be able to return the favor at some points!

Best
Sarah
Reply all
Reply to author
Forward
0 new messages