Hannah Lee
Hannah Lee

Reputation: 75

Making agentset outof list

This is relevant to the previous question just asked.

How can I convert list(which representing turtles) to a agentset?

For example, I want to make

agentset which contains 4 elements [turtle 0 turtle 3 turtle 4 turtle 7]

out of list ["turtle 0" "turtle 3" "turtle 4" "turtle 7"]

I've tried "foreach" before.

Thank you in advance~!!

Upvotes: 1

Views: 1277

Answers (2)

Luke C
Luke C

Reputation: 10291

Are you constrained for some reason to working with a list of strings? If not, but you still want a list, I'd recommend just building your lists to contain agents in the first place. If you're writing this to a file later, it will be converted to string anyway. Internally it's much easier to work with the agents directly.

To make a list of agents based on a list of numbers:

globals [ turtle-list num-list]

to setup 
  ca
  reset-ticks

  crt 3   

  set num-list [ 0 1 2 ]
  set turtle-list map [ i -> turtle i ] num-list 
  print turtle-list      
end

Note that if the list contains a who number for a non-existent turtle, you will get a nobody in your list.

Nevermind all this, just do what @NicolasPayette says.

Then you *can* use `foreach` to easily build your agentset for your `turtle-list`: foreach turtle-list [ t -> set turtle-agentset (turtle-set turtle-agentset t ) ] However, if your end goal is an agentset and you don't need a list, you can skip a step and just build the agentset directly: to setup ca reset-ticks crt 3 set num-list [ 0 1 2 ] set turtle-agentset nobody foreach num-list [ n -> set turtle-agentset (turtle-set turtle-agentset turtle n ) ] print turtle-agentset end

Upvotes: 0

Nicolas Payette
Nicolas Payette

Reputation: 14972

I am not sure why you would need to work with a list like

["turtle 0" "turtle 3" "turtle 4" "turtle 7"]

in the first place. Storing references to agents as anything other than direct references to the agents is usually not a good idea.

That being said, you can convert such a string to an agentset with:

turtle-set map runresult ["turtle 0" "turtle 3" "turtle 4" "turtle 7"]

If any of those turtles don't exist, they will just be excluded from the resulting agentset.

Still, the whole thing strikes me as somewhat ill advised. If you tell us more about what you're trying to accomplish, maybe we could suggest a better approach altogether.

Upvotes: 3

Related Questions