模式匹配键在erlang地图 [英] Pattern matching key in erlang maps
问题描述
Map =#{#{country =>India} => #{rank => 1}}。
我正在尝试匹配如下:
1。 #{Key:= V} = Map。
或
2。 #{#{country:= Country}:=#{rank:= Rank}} =地图。
但它不适合我。有什么帮助,如何做到这一点?
当从映射匹配键值关联时,键表达式必须是具有文字或绑定变量的表达式,请参阅地图文档(部分模式中的地图)。
匹配表达式的问题如下:
#{Key:= $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $一个未绑定的变量是匹配全部地图中的键/值绑定 M
,而不是特定的键/值。与您尝试的其他匹配表达式相同,它可以匹配几个键。
正确的方法是在这里完全指定键,如此
#{#{country => 印度}:= V} =地图。
I have a map of form shown below:
Map = #{#{country=>"India"} => #{rank => 1}}.
I am trying to match it as follows:
1. #{Key := V} = Map.
OR
2. #{#{country := Country} := #{rank := Rank}} = Map.
But its not working for me. Any help as to how it can be done?
解决方案 When matching key-value associations from maps the key expression must be an expression with literals or bound variables, see the documentation of maps (section Maps in Patterns).
The problem with a match expression like:
#{Key := V} = M.
Where Key
is an unbound variable is that this matches all the key/value bindings in the map M
, not a particular key/value. Same with the other match expression you tried, it can match several keys.
The correct way would be to fully specify the key here, like this
#{#{country => "India"} := V} = Map.
这篇关于模式匹配键在erlang地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!