[Elixir] How to remove "" from a string, and convert it a variable?

I have two variables:

iex(1)> i = 1  
iex(2)> user_1 = "Jupeter"
iex(3)> new_variable = "user_" <> "#{i}"
iex(4)> IO.puts new_variable

I wanna get “Jupeter” rather than user_1. Any idea will be appreciated. .

I don’t think this is possible in elixir.


Variable variables as in PHP are not possible in Elixir.

You might be able to craft some variable names during compile time from atoms and using the var!/1 macro, but it’s definitely not possible at runtime.

1 Like

I have tried this, but not works.

user_i = "user_" <> "#{i}"
user_i = String.replace(user_i, ~s("), "")

My goal is to insert 1,000 users into db.

user1 = %User{name: "Jupeter"}
user2 = %User{name: "Superman"}
user3 = %User{name: "Batman"}
user4 = %User{name: "Xman"}
user5 = %User{name: "Joker"}

for i <- [1 .. 5] do
  "user_" <> i = "user_" <> "#{i}"
  Repo.insert! "user_" <> i

any idea?

Use a list.

How to fix the code below using a list?

for i <- [1 .. 5] do
  user = "user_" <> "#{i}"
  user = String.replace(user, ~s("), "")
  Repo.insert! user
for user <- users do

Or even better, use Enum.each/2 instead.

1 Like

I need make a new variable, user_1, from user1, user_2 from user2, and so on. Anyway, thank you, NobbZ.

You can’t. This is Elixir, not PHP.

If you want to use numbered variables, you have to resort to macros and metaprogramming, but in this case this is far from beeing idiomatic. Just have a list with your 1k entries and iterate over it.

Enum.with_index([:a, :b, :c])
[a: 0, b: 1, c: 2]

then you can create your user from the index and you still have access to your user from list

 Enum.with_index([:a, :b, :c])
 |> Enum.each(fn {user, i}->
   Repo.insert!(%User{name: user, id: "user_#{i}"})

I assume you want to have user_name and user_var in the db columns


Wow, I’ll try it. Thank you much. dkuku.

The second argument of Repo.insert!/2 has to be a Keyword.t, but you are passing String.t, that will probably fail. What do you expect that call to do?

you still need a list of user names - how you generate the 1000 users currently? text file?
you may want to read the text file and create the list from that

1 Like


1 Like

Well, my assumption was, that the primary key is already part of the input, or to be generated automatically on insert.

Actually, 50,000,000 users. They are all the South Korean population, and I am coding a quarantine system to track all the daily business transactions of them in Korean markets.

I am making Youtube videos(Korean language) explaining the process of coding the system day by day.

Do you have any idea to generate 50 million users using their social security numbers? I hope to use Google spreadsheets.
Any contribution to the project will be quite appreciated.

Use a library to read from Google. From a quick search, I found google_api_sheets which promises to be able to read and write Google Sheets.

Though now that we are talking about inserting 50 Million records, You should not do that using Repo.insert, but Repo.insert_all instead, in batches of 1 to 5k to reduce number of DB roundtrips.

Though to properly answer your question, we need to know more about how the input data is shaped and how you want to persist them in the database.

Just by knowing some SSIN you can’t create remaining data from thin air…

I am trying to make the code below work. The number of users will be 50 millions.

user1 = %User{name: "Jupeter"}
user2 = %User{name: "Superman"}
user3 = %User{name: "Batman"}
user4 = %User{name: "Xman"}
user5 = %User{name: "Joker"}

users1 = [user1, user2, user3, user4, user5]

for user <- users1 do
  i = 2
  users_1 = []
  user_ = "user_" <> "#{i}"
  user_ = String.replace(user_, ~s("), "")
  user_ = Repo.insert! user
  users_1 = [user_ | users_1]
  i = i + 1

buyer1 = %Buyer{name: "Buyer Jupeter"}
buyer2 = %Buyer{name: "Buyer Superman"}
buyer3 = %Buyer{name: "Buyer Batman"}
buyer4 = %Buyer{name: "Buyer Xman"}
buyer5 = %Buyer{name: "Buyer Joker"}

buyer_1 = Ecto.build_assoc(user_1, :buyers, buyer1)
buyer_2 = Ecto.build_assoc(user_2, :buyers, buyer2)
buyer_3 = Ecto.build_assoc(user_3, :buyers, buyer3)
buyer_4 = Ecto.build_assoc(user_4, :buyers, buyer4)
buyer_5 = Ecto.build_assoc(user_5, :buyers, buyer5)

Repo.insert! buyer_1

You can not dynamically create variable names. Either do it the list way, or repeat your insert calls literally, as you did with variable names already…


You do not want to store your user identifier as a variable name - store it as a map and use Map.get for that
Repo.insert!(Map.fetch(users, “user_1”))