Reputation: 407
I have two lists, both will always be the same length. The first is binary and says whether an agent will move this round, the second contains a set of agents that will need to do something, based on the first list. Something like:
list1 = [0 1 1 1 0]
list2 = [turtle-1 turtle-2 turtle-55 turtle-6 turtle-8]
My objective is to create a third list with only the active turtles in it. Accordingly this list will comprise: turtle-2 turtle-55 and turtle-6. What's the best way to do this?
Upvotes: 2
Views: 719
Reputation: 1894
I am not sure how you can do it with map (Map is cleaner and more efficient) but with foreach you can do it as follow :
to test-lists
let list3 []
foreach list2 [
if (item (position ? list2) list1 = 1)[
set list3 lput ? list3
]
]
print (word "List1 is " list1)
print (word "List2 is " list2)
print (word "List3 is " list3)
end
This is the output:
List1 is [0 1 1 1 0]
List2 is [(turtle 1) (turtle 2) (turtle 55) (turtle 6) (turtle 8)]
List3 is [(turtle 2) (turtle 55) (turtle 6)]
Upvotes: 1
Reputation: 30453
Like this:
map last filter [first ? = 1] (map list list1 list2)
sample run:
observer> crt 10
observer> set list1 [0 1 1 1 0]
observer> set list2 map turtle [1 2 5 6 8]
observer> show map last filter [first ? = 1] (map list list1 list2)
observer: [(turtle 2) (turtle 5) (turtle 6)]
Upvotes: 5