[erlang-questions] Finding records in List

Bob Ippolito bob@REDACTED
Thu Jan 13 11:20:28 CET 2011


On Thu, Jan 13, 2011 at 6:06 PM, Arnaud GARCIA <arnaud74130@REDACTED> 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 ?

Well, it depends on if you know what these things are at compile time or not.

If you are doing this at compile time, you can simply use a list comprehension:

[P || P <- people(), P#person.name =:= Name, P#person.age =:= Age].

-bob


More information about the erlang-questions mailing list