maps string question

Anthony Howe achowe@REDACTED
Wed Oct 13 23:27:06 CEST 2021


On 2021-10-13 17:05, Java House 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

They are identical in content, but not in structure / type.

Using a list-string as a key will be different from using a binary-string.  You
will need to convert depending which key type you use.  Using a mix of
list-string and binary-string type as map keys will probably yield confusing
results.

As I understand binary-strings (more like C-string) are better than list-strings
and I would key on binary-string normally.

> 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).

Try

maps:get(binary_to_list(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>
> 


-- 
Anthony C Howe         	                                       SnertSoft
achowe@REDACTED     Twitter: SirWumpus            BarricadeMX & Milters
http://snert.com/    http://nanozen.snert.com/     http://snertsoft.com/


More information about the erlang-questions mailing list