CastLouis
Splitting a string and get all the values from the map
Hi
i was splitting a string that contains all the keys from a map structure and did a Enum.each to get all the values from the map with map.get after that i joined with Enum.join(“,”)
values_example = String.split(names,",")
|> Enum.each(fn(x) -> Map.get(claims,x) end)
|> Enum.join(",")
Is it the best way to archive this goal?
First Post!
cevado
Last Post!
radar
Using your example here:
claims = %{"first" => "First claim", "second" => "Second claim"}
names = "first,second"
String.split(names,",")
|> Enum.map(fn(x) -> Map.get(claims,x) end)
|> Enum.join(",")
Output: "First claim,Second claim"
This will only work if the keys in claims are strings, as you mentioned earlier. If the keys were not strings but instead were atoms, then you would need convert them, probably doing something like:
keys = for name <- String.split(names,","), do: String.to_atom(name)
(I’m going to assume that they’re strings.)
However, I think this way is cleaner:
keys = names |> String.split(",")
claims |> Map.take(keys) |> Map.values |> Enum.join(",")
Output: "First claim,Second claim"
0
Popular in Discussions
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
New
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
Hello everyone,
I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
Please, let me know if this kind of discussion already took place in another topic
.
Hi all, how do you consider if is better to build ...
New
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
Other popular topics
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Chat & Discussions>Discussions
Latest on Elixir Forum
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
- #api
- #forms
- #metaprogramming
- #security
- #hex









