Re: maps string question
Roberto Ostinelli <[email protected]>
| Newsgroups | gmane.comp.lang.erlang.general |
|---|---|
| Message-ID | <[email protected]> |
<<“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 <[email protected]> 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> >