Changing pid to Map key

I have a process running and it has some associative values. For example process #PID<0.258.0> has some values as {1, 2, 3}.

I want to represent this PID as a key in a map, like this

map = %{#PID<0.258.0>: {1, 2, 3}}

I have tried to_atom and to_string but it doesn’t work. What can I do to make a process id an atom or key in a map.

Also, there is one way of string interpolation but I want to avoid that.


Well, You could use the pid as key… no need to translate.

iex> pid = self()
iex> %{pid => {1, 2, 3}}
%{#PID<0.918.0> => {1, 2, 3}}

Thats so simple and elegant, thank you so much.

You may want to consider using a Registry. That saves you from tracking when those pids exit.

1 Like