Reputation: 105
I am trying to port an older code to the latest (6.0.1), the following foreach loop works fine on Netlogo 4.1.3 but when copy and paste the code into version 6.0.1 the "item 0 ?" does not work. It says the "?" is undefined. That line of code is suppose to retrieve the item of the list inside the segment
to setup-row [row colour segments]
foreach segments
[
if pycor = row * row-patches-width and
(pxcor >= col-patches-width * (item 0 ?)) and (pxcor <= col-patches-
width * (item 1 ?))
[set pcolor colour
output-print item 0 ?]
]
end
The passed in "segments" variable contains the following lists:
setup-row 4 blue [[-8 -5] [-3 -1] [0 3] [5 9]]
If the code is working correctly, it should retrieve the -8 with (item 0 ?) and -5 with (item 1 ?) and so on. What I assumed in the old code is that the "?" are the first list retrieved from segments which is [-8 -5] and (item 0 ?) retrieved the -8 and (item 1 ?) retrieved the -5.
I have tried to read through the new user manual to find something that works similarly but to no avail or maybe I didn't look in the right place. Hope some of you can point me in the right direction.
Upvotes: 0
Views: 2359
Reputation: 10291
Yep, that's been changed in Netlogo 6.0- see the transition guide page. For more detail on the new foreach
syntax, see the dictionary entry. Basically, instead of using ?
now, you explicitly define the variable names to use in the foreach
procedure. Following your list example above:
to foreach-example
let ex [ [-8 -5] [-3 -1] [0 3] [5 9] ]
foreach ex [
[ xy_coords ] ->
ask patches with [ pxcor = (item 0 xy_coords) and pycor = ( item 1 xy_coords) ] [
set pcolor white
]
]
end
Here, I explicitly state that the list items will be called "xy_coords" throughout the procedure. It's a nice change that allows for more readable code since your variables can have more meaningful names.
Upvotes: 5