amnu3387

amnu3387

How to use strings as keys on Maps?

Hi,

so when I Map.put even if I use the string quotation marks on the key Elixir will render the map with atoms. This is further problematic when I save data into the db as JSON, because when retrieving this data, it is all as string keys and no atoms.

Amap
|> Map.put("player_#{number}", %{
      roll: false,
      id: player.id,
      stops_self: %{
        "aether_charge": true
     }})

The real use case is that, when I first create this map, it’s through an individual process, that ultimately, saves it to the db, so if the process gets killed or crashes, when being served again, the db version will be fetched.
Is there any way I can enforce a map to use string keys from the get go? So as to always have to deal with a single type of keys. I’m aware I could write a module that returns either atom or string keys but is there any easy way to be explicit about what type I want my key to be?

Thanks

Marked As Solved

NobbZ

NobbZ

You are not using a string, but quoted atom. Those are meant to be used, when you have symbols which usually can’t be in a simple atom literal: :"$end_of_table". If you want to use a string as key in a map do it like this: %{"key" => 1}.

Also Liked

amnu3387

amnu3387

@kelvinst if I have to be sincere I don’t remember what was the title, but this seems like pretty much to the point :stuck_out_tongue:

amnu3387

amnu3387

LOL then this is much better definitively ^^

Where Next?

Popular in Questions Top

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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
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
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
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 45766 226
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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

We're in Beta

About us Mission Statement