I have defined 2 records:
-record(state, {port = 9921,
clients = []
}
).
-record (client, {pid,
acc}).
And I have created variable which contains port and 3 records:
State = #state{port = 9921,
clients = []},
NewClient1 = #client{pid = "A", acc = <<85>>},
NewClient2 = #client{pid = "B", acc = <<73>>},
NewClient3 = #client{pid = "C", acc = <<56>>},
NewState = State#state{clients = [NewClient1 , NewClient2, NewClient3]},
NewState now contains
#state{port = 9921,
clients = [#client{pid = "A",acc = <<"U">>},
#client{pid = "B",acc = <<"I">>},
#client{pid = "C",acc = <<25>>}]}
My question is, I want to search record state for some specific pid, example: I want to get true for function find ("B", NewState) and false for function find ("Z", NewState). What is most easiest way to do it?
2条答案
按热度按时间kg7wmglp1#
您可以使用事实,即
#client.pid
在记录元组中包含pid
的索引。因此,最简单、最高效的解决方案(最多100个客户端,则应更改
#state.clients
的数据格式以Map或使用ets)是请参阅
yhxst69z2#
记录语法允许您使用以下命令访问客户端列表:
那么你可以使用函数lists:any/2来检查一个列表中是否至少有一个元素的条件为真:
综合起来