Reputation: 3
I have defined my turtles with the following.
turtles-own[val1, posx,posy,value]
And I have define globals called
xlist ylist vallist
That I start empty. Now I want to create list that for any turtle have one value.
I did
to fillLists
set xlist lput posx xlist
set ylist lput posy ylist
set vallist lput value vallist
end
And this is called with:
ask turtles[fillLists]
For example If I have 3 turtles with val1 be a name Let's say we have:
t1 =[Mike, 1, 10, 100]
t2 =[Sasha, 2, 20, 200]
t3 =[Rocco, 3, 30, 300]
I would like to create list l1,l2,l3,l4. Where the contents are:
l1= [Mike, Sasha, Rocco]
l2= [1,2,3]
l3= [10,20,30]
l4= [100,200,300].
But what i tried don't work. Because the list return empty. What is happening, is some bug?
Upvotes: 0
Views: 122
Reputation: 10291
I can't reproduce your error, so you may need to include more of the code where these commands are called. For example, if I run the setup
as defined here:
globals [ nameslist xlist ylist vallist ]
turtles-own [ val1 posx posy value ]
to setup
ca
set nameslist []
set xlist []
set ylist []
set vallist []
crt 1 [ set val1 "Mike" set posx 1 set posy 10 set value 100 ]
crt 1 [ set val1 "Sasha" set posx 2 set posy 20 set value 200 ]
crt 1 [ set val1 "Rocco" set posx 3 set posy 30 set value 300 ]
ask turtles [ fill-lists ]
print nameslist
print xlist
print ylist
print vallist
reset-ticks
end
to fill-lists
set nameslist lput val1 nameslist
set xlist lput posx xlist
set ylist lput posy ylist
set vallist lput value vallist
end
I get an output like:
[Mike Rocco Sasha]
[1 3 2]
[10 30 20]
[100 300 200]
However, you may find that it's simpler to just use of
to query the turtles for your variable of interest. For example the code below accomplishes more or less the same output, although its use may depend on your need:
turtles-own [ val1 posx posy value ]
to setup
ca
crt 1 [ set val1 "Mike" set posx 1 set posy 10 set value 100 ]
crt 1 [ set val1 "Sasha" set posx 2 set posy 20 set value 200 ]
crt 1 [ set val1 "Rocco" set posx 3 set posy 30 set value 300 ]
print [val1] of turtles
print [posx] of turtles
print [posy] of turtles
print [value] of turtles
reset-ticks
end
Output:
[Sasha Mike Rocco]
[2 1 3]
[20 10 30]
[200 100 300]
Upvotes: 1