coilardium

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

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, %{}))

Also Liked

gregvaughn

gregvaughn

The last line can be simplified to traverse the list only once with:

Enum.map(values, &Map.new(Enum.zip(keys, &1)))

Where Next?

Popular in Questions Top

vertexbuffer
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
Darmani72
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
yawaramin
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
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
shahryarjb
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
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
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

Other popular topics Top

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
joaquinalcerro
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
greenz1
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
axelson
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...
239 48475 226
New
shahryarjb
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
belgoros
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
New
freewebwithme
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
albydarned
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

We're in Beta

About us Mission Statement