Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

searching in a list for a record with specific fields and ignoring the rest

Tags:

erlang

I have a record defined as:

1> rd(fact, {a,b,c}).
fact

I create three records and put them in a list

2> F1 = #fact{a=1,b=1,c=1}.
#fact{a = 1,b = 1,c = 1}
(3> F2 = #fact{a=2,b=2,c=2}.
#fact{a = 2,b = 2,c = 2}
3> F3 = #fact{a=3,b=3,c=3}.
#fact{a = 3,b = 3,c = 3}
4> L = [F1,F2,F3].
[#fact{a = 1,b = 1,c = 1},
 #fact{a = 2,b = 2,c = 2},
 #fact{a = 3,b = 3,c = 3}]

Now, I want to check if the list contains a record in which 'a' is 1 and I don't care for the rest of the fields

(dilbert@Martin-PC)21> lists:member(#fact{a=1}, L).
false
(dilbert@Martin-PC)23> lists:member(#fact{a=1,b=1,c=1}, L).
true

How can I accomplish it?

like image 783
Martin Dimitrov Avatar asked Jan 17 '26 04:01

Martin Dimitrov


1 Answers

Or you could use keyfind.

lists:keyfind(1, #fact.a, L).
like image 124
lixen Avatar answered Jan 19 '26 18:01

lixen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!