0

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.

Jon Riel
  • 329
  • 2
  • 17
  • 2
    Possible duplicate of [Erlang lists:index\_of function?](https://stackoverflow.com/questions/1459152/erlang-listsindex-of-function) – Dogbert Apr 12 '18 at 19:01

1 Answers1

1

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)
RichardC
  • 10,412
  • 1
  • 23
  • 24