Search code examples
listattributesnetlogopatch

Netlogo: how to assign list values to patches attributes


i am new to Netlogo, so i apologize if this is a trivial question. I would like to assign/copy the values of a list into patches attribute, making sure that the order of the values is respected. The following code does the opposite, that is, it copies the values of a patches attribute (attr1) into list1

patches-own [ attr1 attr2]
to setup
  clear-all
  ask patches [set attr1 random 10]  
  let patch-list sort patches
  let list1 map [ p -> [attr1] of p ] patch-list
end

now, say that I would like to assign/copy the values of varX into patches attribute attr2, making sure that the order of the values is respected.

  let nPix world-width * world-height 
  let varX (range nPix)
  
  ????

any suggestion? thanks


Solution

  • You can let Netlogo iterate through two lists simultanously by feeding the anonymous procedure two inputs. To do that, you need to put brackets around the entire procedure.

    patches-own [attr2]
    
    to setup
      
      ca
      resize-world 0 4 0 4
      
    end
    
    to go
      
      let nPix world-width * world-height 
      let varX n-values nPix [random 140]
    
      (foreach sort patches varX [ [the-patch the-var] ->
        ask the-patch [set attr2 the-var]
        ]
      )
    
      show varX
      ask patches [set pcolor attr2]
      
    end
    

    You can also always opt for a good old while loop where you increment an index (i) within the loop and use that index to couple the corresponding items from both lists.

    to go-3
      
      let nPix world-width * world-height 
      let varX n-values nPix [random 140]
      
      let patch-list sort patches
      
      let i 0
      while [i < nPix] [
        ask item i patch-list [set attr2 item i varX]
        set i i + 1
      ]
      
      show varX
      ask patches [set pcolor attr2]
      
    end