coilardium
Converting list with strings values to maps
I have this list
["description status maker_id checker_id inserted_at updated_at\r",
"Tanker A 2 2 2021-06-10 12:19:19.000 2021-06-10 12:19:19.000\r",
"Open A 2 2 2021-06-10 12:19:19.000 2021-06-10 12:19:19.000\r",
"Flat A 2 2 2021-06-10 12:19:19.000 2021-06-10 12:19:19.000\r",
"Closed A 2 2 2021-06-10 12:19:19.000 2021-06-10 12:19:19.000\r", "\f"]
I want to convert it be like this
[
%{"description: "Tanker", status: "A" maker_id: "2" checker_id: "2" inserted_at: "2021-06-10 12:19:19.000" updated_at: "2021-06-10 12:19:19.000"},
%{"description: "Open", status: "A" maker_id: "2" checker_id: "2" inserted_at: "2021-06-10 12:19:19.000" updated_at: "2021-06-10 12:19:19.000"},
%{"description: "Flat", status: "A" maker_id: "2" checker_id: "2" inserted_at: "2021-06-10 12:19:19.000" updated_at: "2021-06-10 12:19:19.000"},
%{"description: "Closed", status: "A" maker_id: "2" checker_id: "2" inserted_at: "2021-06-10 12:19:19.000" updated_at: "2021-06-10 12:19:19.000"}
]
Marked As Solved
moogle19
This should work:
list = ["description status maker_id checker_id inserted_at updated_at\r",
"Tanker A 2 2 2021-06-10 12:19:19.000 2021-06-10 12:19:19.000\r",
"Open A 2 2 2021-06-10 12:19:19.000 2021-06-10 12:19:19.000\r",
"Flat A 2 2 2021-06-10 12:19:19.000 2021-06-10 12:19:19.000\r",
"Closed A 2 2 2021-06-10 12:19:19.000 2021-06-10 12:19:19.000\r", "\f"]
[keys | values] = Enum.map(list, &String.split/1)
values |> Enum.map(&Enum.zip(keys, &1)) |> Enum.map(&Enum.into(&1, %{}))
2
Also Liked
gregvaughn
The last line can be simplified to traverse the list only once with:
Enum.map(values, &Map.new(Enum.zip(keys, &1)))
4
Popular in Questions
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
I would like to know what is the best IDE for elixir development?
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function:
-dialyzer...
New
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
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
For example for a current url like
http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2,
I would like to get:
...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Other popular topics
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
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









