dokuzbir

dokuzbir

How to convert list to string without losing brackets

Hello,

I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these

buyer = %{
id: “BY789”,
name: “Micheal”,
surname: “test”,
}
address =%{
contactName: "test tes ",
city: “test”,
country: “test”,
address: “test”,
}
item = %{
id: “BI101”,
category: “test”,
Type: “PHYSICAL”,
price: “0.3”
}

I want to convert them to like that

“[buyer=[id=BY789,name=Micheal, surname=test],address=[contactName=test,**
city=test,country=test,address=test],item=[id=BI101,category=test,Type=PHYSICAL, price=0.3]]”

i tried that but it is not like what i want.

Pre_string = for map ← [buyer,address, item] do
to_string(Enum.map(map, fn {k, v} → “#{k}=#{v},” end)
end
Enum.chunk_every(Pre_string, 1)

result is that

[ [“id=BY789,name=Micheal, surname=test”, “contactName=test],
[city=test,country=test,address=test”], [“id=BI101,category=test,Type=PHYSICAL, price=0.3”]]

My problem is losing map titles and when i convert that to string losing brackets

Marked As Solved

peerreynders

peerreynders

defmodule Demo do

  defp to_string_(%{} = m) do
      m
      |> Map.to_list()
      |> to_string_()
  end
  defp to_string_(l) when is_list(l) do
    values =
      l
      |> Enum.map(&to_string_/1)
      |> Enum.join(",")

    "[#{values}]"
  end
  defp to_string_({k,v}) when is_atom(k) do
    "#{Atom.to_string(k)}=#{to_string_(v)}"
  end
  defp to_string_(s) when is_binary(s) do
    s
  end

  def convert(l) when is_list(l) do
    to_string_(l)
  end

end

buyer = %{
  id: "BY789",
  name: "Micheal",
  surname: "test"
}
address = %{
  contactName: "test tes ",
  city: "test",
  country: "test",
  address: "test"
}
item = %{
  id: "BI101",
  category: "test",
  type: "PHYSICAL",
  price: "0.3"
}

IO.inspect(
  Demo.convert([buyer: buyer, address: address, item: item])
)
$ elixir demo.exs
"[buyer=[id=BY789,name=Micheal,surname=test],address=[address=test,city=test,contactName=test tes ,country=test],item=[category=test,id=BI101,price=0.3,type=PHYSICAL]]"

Also Liked

peerreynders

peerreynders

But do you understand how it works … ?
The code is worthless - understanding is priceless.

Schultzer

Schultzer

use Kernel.inspect like

iex> "#{inspect ["abc", 1, 3.14, %{}, {}]}"
"[\"abc\", 1, 3.14, %{}, {}]"
Schultzer

Schultzer

But those are just escaped chars.? you could always convert it to a list then they won’t show.

NobbZ

NobbZ

They are not actually there… Please read about escaping strings in programming, the concept is universal.

dokuzbir

dokuzbir

thanks for interesting approach but now i have trouble with backslashes. :slight_smile:

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement