erlang 如何遍历列表中的Map,并将其值存储到变量中

gg0vcinb  于 2022-12-08  发布在  Erlang
关注(0)|答案(2)|浏览(204)

I'm very new to erlang and i'm trying to do something like this:
There is a list of maps e.g.:

List = [#{id => 1, start=> 'Y', memberName => "MemberName" },
          #{id => 2, start=> 'N', memberName => "MemberName" }],
  • EDITED*

and then I want to iterate through each of the map in the List and store each value of memberName and id to a separate variable.
How to achieve something like that?

ax6ht2ek

ax6ht2ek1#

in this case, you can also use list comprehension and pattern maching:

1> List = [#{id => 1, start=> 'Y', memberName => "MemberName1" },#{id => 2, start=> 'N', memberName => "MemberName2" }],
2> [X || #{memberName := X} <- List].
["MemberName1","MemberName2"]
3>

Note that the main behavior difference compare to @2240 solution is that it will not fail if the list contains different element. I can't know what is your expectation in this case.

3> L2 = List ++ [atom,#{nomembername => no}].                       
[#{id => 1,memberName => "MemberName1",start => 'Y'},
 #{id => 2,memberName => "MemberName2",start => 'N'},
 atom,
 #{nomembername => no}]
4> [X || #{memberName := X} <- L2].                                 
["MemberName1","MemberName2"]
5> lists:foldl(fun(Elem, AccIn) -> lists:append([maps:get(memberName, Elem)], AccIn) end,  [], List).
["MemberName2","MemberName1"]
6> lists:foldl(fun(Elem, AccIn) -> lists:append([maps:get(memberName, Elem)], AccIn) end,  [], L2).  
** exception error: {badmap,atom}
     in function  maps:get/2
        called as maps:get(memberName,atom)
     in call from erl_eval:do_apply/6 (erl_eval.erl, line 684)
     in call from erl_eval:expr/5 (erl_eval.erl, line 232)
     in call from erl_eval:expr_list/6 (erl_eval.erl, line 888)
     in call from erl_eval:expr/5 (erl_eval.erl, line 411)
     in call from lists:foldl/3 (lists.erl, line 1263)
7>

[EDIT]

I am not sure to understand what you want to do. In erlang, a list is a very basic structure used to sore "multiple" results. Then, you can work with its element using one of the many list iterator: foreach, map, foldl, foldr, mapfoldl, mapfoldr or build your own recursive function.
To go further in your direction, there are 2 cases, depending on the fact that your initial list has a fixed length known when you write and compile your application.
If yes, and it seems very unlikely, than you can simply match the result list to a variable list as in this code:

8> [Name1,Name2] = [X || #{memberName := X} <- List].                                                
["MemberName1","MemberName2"]
9> Name1.
"MemberName1"
10> Name2.
"MemberName2"
11>

If the length of the list may vary or is unknown, it is not possible to assign each name to an individual variable. You will have to assign the result to a list of name and then work with this list, or traverse the initial list of maps and work on a single name at a time. Here is an example using lists:foreach/2:

Names = [X || #{memberName := X} <- List],
lists:foreach(fun(X) -> do_something(X) end,Names),
...
or
lists:foreach(fun(#{memberName := X}) -> do_something(X) end,List),
...
uhry853o

uhry853o2#

您可以使用折迭来执行此操作。

> List = [#{id => 1, start=> 'Y', memberName => "MemberName" },
          #{id => 2, start=> 'N', memberName => "MemberName" }],
> lists:foldl(fun(Elem, AccIn) -> lists:append([maps:get(memberName, Elem)], AccIn) end,  [], List).
["MemberName","MemberName"]

相关问题