maps string question

Java House java4dev@REDACTED
Thu Oct 14 00:19:06 CEST 2021


Thank you Roberto and Mark for the clarification.
I am loading the data from a file using the file:consult(MapData)
Currently the file looks like this
{"key1", {"data1"}}.
{"key2", {"data2"}}.

If I want to have all binary for consistency does this means that I need to
write into the file as
{<<"key1">>, {<<"data1">>}}.
{<<"key2">>, {<<"data2">>}}.

Kind Regards
Nikolas

Στις Τετ, 13 Οκτ 2021 στις 11:24 μ.μ., ο/η Roberto Ostinelli <
ostinelli@REDACTED> έγραψε:

> <<“key”>> is a binary, “key” is a list, so they are not the same.
> maps:get/2 raises because the key is not found.
>
> > string:equal(Key1, Key11).
>
> The string representation for both is the same, hence this return true.
>
> Hope this helps,
> r.
>
> > On 13 Oct 2021, at 23:05, Java House <java4dev@REDACTED> wrote:
> >
> > 
> > Hello
> > I am having a problem with maps and strings as keys.
> > My understanding that the the two string formats "string" and
> <<"string">> are identical but as you can see from the transcript bellow
> maps does not think so.
> > I searched but I couldn't find any function to convert form "string" to
> <<"string">> or the other way around.
> > How can I make maps to accept both format of strings?
> >
> > Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:8:8] [ds:8:8:10]
> [async-threads:1] [jit]
> >
> > Eshell V12.0  (abort with ^G)
> > 1> Key1 = "key1".
> > "key1"
> > 2>
> > 2> Key11 = <<"key1">>.
> > <<"key1">>
> > 3>
> > 3> string:equal(Key1, Key11).
> > true
> > 4>
> > 4> Map = maps:new().
> > #{}
> > 5>
> > 5> Map1 = maps:put("key1", {something}, Map).
> > #{"key1" => {something}}
> > 6>
> > 6> maps:get(Key1, Map1).
> > {something}
> > 7>
> > 7> maps:get(Key11, Map1).
> > ** exception error: bad key: <<"key1">>
> >      in function  maps:get/2
> >         called as maps:get(<<"key1">>,#{"key1" => {something}})
> >         *** argument 2: not a map
> > 8>
> > 8> maps:get("key1", Map1).
> > {something}
> > 9>
> > 9> maps:get(<<"key1">>, Map1).
> > ** exception error: bad key: <<"key1">>
> >      in function  maps:get/2
> >         called as maps:get(<<"key1">>,#{"key1" => {something}})
> >         *** argument 2: not a map
> > 10>
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20211014/b9cd1cef/attachment.htm>


More information about the erlang-questions mailing list