foreach

foreach list [ commands ]
(foreach list1 ... [ commands ])

With a single list, runs commands for each item of list. In commands, use ? to refer to the current item of list.

foreach [1.1 2.2 2.6] [ show (word ? " -> " round ?) ]
=> 1.1 -> 1
=> 2.2 -> 2
=> 2.6 -> 3

With multiple lists, runs commands for each group of items from each list. So, they are run once for the first items, once for the second items, and so on. All the lists must be the same length. In commands, use ?1 through ?n to refer to the current item of each list.

Some examples make this clearer:

(foreach [1 2 3] [2 4 6]
   [ show word "the sum is: " (?1 + ?2) ])
=> "the sum is: 3"
=> "the sum is: 6"
=> "the sum is: 9"
(foreach list (turtle 1) (turtle 2) [3 4]
  [ ask ?1 [ fd ?2 ] ])
;; turtle 1 moves forward 3 patches
;; turtle 2 moves forward 4 patches

See also map, ?.

Take me to the full NetLogo Dictionary