简体   繁体   中英

Find the position of a field in a list Erlang

I have this list

[id,container,feed_id,prev,next,feeds,name,street,street_no,
 firstname,lastname,email,password,phone...]

and I want to get the position of various elements, like name, which would be 7.

How do I get the position of elements in a list with Erlang?

lists:member(name, List).

This only returns whether an element is part of the list.

If it's likely that you want to look up the index for several items, or even all, and perhaps also do this repeatedly, it would be best to create an index mapping:

Map = maps:from_list(lists:zip(List, lists:seq(1, length(List))))

You can then keep this map around, and use it like this:

Pos = maps:get(Element, Map)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM