[erlang-questions] Finding records in List

Bengt Kleberg <>
Thu Jan 13 11:20:42 CET 2011


It sounds as if you want to filter a list of person records (Persons).
That means either lists:filter/2 or a list comprehension.

Both approaches can use the following function:

is_name_and_age( Person, Name, Age ) ->
	(Person#person.name =:= Name) andalso (Person#person.age =:= Age)

and the list comprehension would be:

[X || X <- Persons, is_name_and_age( X, Name, Age )]


On Thu, 2011-01-13 at 11:06 +0100, Arnaud GARCIA wrote:
> Hello,
> (still learning erlang ;-)
> I would like to find all records matching criterion/criteria in a list of
> records.
> if you have one criteria, you can use:
>  lists:keyfind(Name,#person.name,ListOfPersonRecords).
> => But, it will return just the first one who match the criteria
> =>Moreover with lists:keyfind I didn't find a way for multiple criteria like
> : find all person in list where Name= and Age=
> I saw an interesting post using record<->proplists... is it the way to do it
> ?
> http://stackoverflow.com/questions/447188/erlang-and-run-time-record-limitations
> Finally, I build my own function which parse the List and check if it match
> the criterion or criteria BUT I was asking if there is an erlang function to
> do this ?
> thanks,
> Arnaud

More information about the erlang-questions mailing list