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?
2条答案
按热度按时间ax6ht2ek1#
in this case, you can also use list comprehension and pattern maching:
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.
[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:
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:
uhry853o2#
您可以使用折迭来执行此操作。