md-86
md-86

Reputation: 83

How to get element from an erlang list?

I have a list List = [ins_appServer_APP02@mdiaz,ins_appServer_APP04@mdiaz].

and I have an atom that comes as a parameter: AppServerAtom = ins_appServerAPP02@mdiaz

I need help to search in List the element that match with AppServerAtom

It is possible to do this with Erlang?

Upvotes: 0

Views: 2131

Answers (2)

BlackMamba
BlackMamba

Reputation: 10254

If you want get the matched element, you can use lists:filter

lists:filter(fun(X) -> X == AppServerAtom end, List).

If you just want check the element is in the List, you can use lists:member

Upvotes: 0

nu-ex
nu-ex

Reputation: 681

Use lists:member/2:

List = ['ins_appServer_APP02@mdiaz', 'ins_appServer_APP04@mdiaz'],
case lists:member('ins_appServer_APP02@mdiaz', List) of
  true -> do_something_when_true();
  false -> do_something_when_false()
end.

See http://erldocs.com/current/stdlib/lists.html?i=0&search=lists:mem#member/2 in the Erlang function reference.

Upvotes: 3

Related Questions