elixir - Pattern-matching a map with string as key -


i created map string "2" 1 of keys:

iex(14)> map = %{:a => 1, "2" => 2, :b => 3} %{:a => 1, :b => 3, "2" => 2} 

now i'm not able pattern-match it. instance, how value associated "2"? tried following got below error:

iex(23)> %{a: c, "2" z} = map ** (syntaxerror) iex:23: syntax error before: "2"  iex(23)> %{a: c, "2": z} = map ** (matcherror) no match of right hand side value: %{:a => 1, :b => 3, "2" => 2} 

you have remember when key not atom can't use syntax a: value, have explicitly use map syntax: "a" => value.

also what's important can't use atom syntax before =>, so:

 %{:a =>  a,"2" => value} = map # valid, everywhere use =>  %{"2" => value, a: a} = map  # valid, atom syntax after regular 

but 1 invalid:

 %{a: a, "2" => value} = map  ** (syntaxerror) iex:5: syntax error before: "2" 

my suggestion: when mixing atoms , strings keys clarity use regular syntax.


Comments