# How to programatically name agents?

**URL:** <https://forum.elixirforum.com/t/how-to-programatically-name-agents/12365>\
**Category:** Questions\
**Tags:** agent\
**Created:** [February 8, 2018, 10:33pm UTC](https://forum.elixirforum.com/t/how-to-programatically-name-agents/12365 "2018-02-08T22:33:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hunar](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/h/c0e974/32.png) [@hunar](https://forum.elixirforum.com/u/hunar)\
**Post date:** [February 8, 2018, 10:33pm UTC](https://forum.elixirforum.com/t/how-to-programatically-name-agents/12365/1 "2018-02-08T22:33:10Z")

</div>

Hi I’m an Elixir beginner,

I have been experimenting with agents, basically I was able to create several agents, naming each with unique atoms and changing their internal states. However, now I’m trying to programmatically name the created agents as this would be useful if I were trying to create 1000 agents.  
Initially my intention was to use something like Enum.range(1…1000) to creaete 1000 agents and then programatically assign the name of each Agent with the corresponding number so that I can call each Agent by their number: Agent.get(23, fn(x)-\> x+1 end), for example to increment agent number 23.  
I realised that Elixir doesn’t allow agents to be named by number!

Can someone please tell me how can I do the above? or if there is an easier way to programatically name 1000 agents and retrieve them? I don’t mind if this was doable by using atoms or even pids as well.

Thanks a lot in advance  
Hunar

---

<div class="post-metadata">

**Author:** ![mariosangiorgio](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mariosangiorgio/32/2380_2.png) [@mariosangiorgio](https://forum.elixirforum.com/u/mariosangiorgio)\
**Post date:** [February 8, 2018, 10:53pm UTC](https://forum.elixirforum.com/t/how-to-programatically-name-agents/12365/2 "2018-02-08T22:53:17Z")

</div>

You could use a [Registry](https://hexdocs.pm/elixir/Registry.html) and give explicit names to your agents.

Something like this will create you 1000 Agents, giving them an unique name.

```elixir
Registry.start_link(keys: :unique, name: MyAgents)
1..1000
|> Enum.map(fn i -> Agent.start_link(fn -> 0 end, name: {:via, Registry, {MyAgents, i}}) end)

```

As explained in the documentation, you ca use the name to interact with them:

```elixir
Agent.get({:via, Registry, {MyAgents, 1}}, & &1)
Agent.update({:via, Registry, {MyAgents, 1}}, & &1 + 1)
Agent.get({:via, Registry, {MyAgents, 1}}, & &1)

```

If you want a more in-depth explanation, I found [this post](https://medium.com/elixirlabs/registry-in-elixir-1-4-0-d6750fb5aeb) to be quite good.

---

<div class="post-metadata">

**Author:** ![hunar](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/h/c0e974/32.png) [@hunar](https://forum.elixirforum.com/u/hunar)\
**Post date:** [February 9, 2018, 10:54am UTC](https://forum.elixirforum.com/t/how-to-programatically-name-agents/12365/3 "2018-02-09T10:54:04Z")

</div>

huge thanks. That is what I wanted 🙂
