[erlang-questions] index for lists and tuples?

Steve Kirsch steve.kirsch@REDACTED
Fri Apr 17 07:16:31 CEST 2009


I am looking for an index function for lists and tuples that returns the
index of the first element that matches a query term.

For example:

index({a, b, c}, b) would return 2

index([a, b, c], c) would return 3

But I couldn't seem to find a function like this either for lists or
tuples. Sure, I could write one, but am I just looking in the wrong
place? This seems like a pretty basic function. 

In my case, I wanted to make a tuple of logging levels like {all, info,
warn, errors, none} and then I can compare the atom supplied by the log
request and compare its numeric index into this tuple to index value of
the requested logging level to determine whether to log or not. I was
surprised not to find a simple index function.

In Python, you can write:
[6,7,8,9].index(8)

which would return 2 (since they start numbering at 0).



More information about the erlang-questions mailing list