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

chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

We're in Beta

About us Mission Statement