I'm not able to assign the first item value of a list to a turtle variable. For example? I have list3 that has 1 item, that is: list3: [[5 2]]
I would like the first value of list 3 item 0 to be assigned to reproduction (ie 5) and the second value of list 3 item 0 to be assigned to metabolism (ie 2).
How can I do this?
Thanks in advance
globals [ ValidHabsItem ValidHabs ItemList3 ]
turtles-own [ profiles-habitat metabolism reproduction ]
to setup
let list1 ( list 2 )
let list2 ( list 5 )
let list3 ( reduce sentence ( map [ i -> map [ j -> list j i ] list2 ] list1 ) )
print ( word "list3: " list3 )
let n0 count turtles
let s length list3 * 10
while [ n0 < s ]
[
let c count patches
if c = 0 [ stop ]
ask one-of patches
[
sprout 1
[
set profiles-habitat item ValidHabsItem ValidHabs
set reproduction item ItemList3 list3 ;; list3: [[5 2]];; value 5
set metabolism item ItemList3 list3 ;; list3: [[5 2]];; value 2
]
]
]
set n0 count turtles
end
You can easily understand how to do this by playing in the Command Center and seeing how NetLogo responds to your calls to the list.
Just create a global variable in the Code tab, as globals [my-list]
, and then follow my statements in the Command Center:
observer> set my-list [[1 2 3] [4 5 6] [7 8 9]]
observer> print my-list
[[1 2 3] [4 5 6] [7 8 9]]
So here we just created a list whose items are other lists.
Therefore, we have:
observer> print first my-list
[1 2 3]
observer> print last my-list
[7 8 9]
observer> print item 1 my-list
[4 5 6]
This means that now, for NetLogo, the whole first my-list
statement is equal to [1 2 3]
; in other words, it is a list containing those numbers. So you can treat first my-list
as any other list. For example, if you want to extract the second item from that list, you will simply do:
observer> print item 1 (first my-list)
2
The parentheses there are optional: they are useful to make the statement more readable to humans, reminding us that the whole first my-list
is a list from which we are extracting the second item (i.e. the item with index 1). However, NetLogo does not really need that:
observer> print item 1 first my-list
2
Now you should be able to easily solve your problem, because your case is easier than this: your list3
, which in your example is [[5 2]]
, only has one item in it. This means that first list3
is equal to last list3
which is equal to item 0 list3
, all of them being [5 2]
.
So there you can do:
set reproduction first (first list3)
set metabolism last (first list3)
In general, you can apply this mechanisms to how many levels of nesting you wish.
For example:
observer> set my-list [[[1 2] [3 4]] [[5 6] [7 8]]]
observer> print my-list
[[[1 2] [3 4]] [[5 6] [7 8]]]
observer> print first my-list
[[1 2] [3 4]]
observer> print last (first my-list)
[3 4]
observer> print first (last (first my-list))
3
PS:
Note that, in your while
loop, you need to include set n0 count turtles
within the command block (as in while [condition] [commands]).
The way you have it now, set n0 count turtles
is outside the command block of the loop and this means that the loop will go on forever, because it will never get to the point where n0
is updated and so n0 < s
will always evaluate as true.
Also, note that saying
let c count patches
if c = 0 [ stop ]
makes no sense in NetLogo: every model will always have at least one patch, so you will never get to the point of having no patches.
Maybe you wanted to say something like if (count patches with [not any? turtles-here] = 0) [stop]
? Just guessing, but here we are going off-topic