erlang记录麻烦
我正在努力处理我的一个模块中的记录。
我在代码顶部定义了一条记录:
-record(user, {pid,
name,
nick}).
简而言之,每个用户将被表示为具有自己的 pid 和其他字段的进程。
稍后在模块中我将执行以下操作:
Pid = UserPid,
GetUser = fun(X) ->
if X#user.pid =:= Pid -> true;
X#user.pid=/= Pid -> false
end
end,
User = lists:filter(GetUser, Users),
io:format("User pid is ~p~n",[User#user.pid]).
运行此代码我得到:
** exception error: {badrecord,user}
但如果我这样做:
io:format("User ~p~n",[User]).
它会打印
User [{user,<0.33.0>,name1,nick1}]
任何人都可以指出我缺少什么吗?
谢谢
I'am struggling with records in one of my modules.
I defined on top of my code a record as:
-record(user, {pid,
name,
nick}).
in few words each user is going to be represented as process with its own pid and other fields.
Later on in the module I am doing the following:
Pid = UserPid,
GetUser = fun(X) ->
if X#user.pid =:= Pid -> true;
X#user.pid=/= Pid -> false
end
end,
User = lists:filter(GetUser, Users),
io:format("User pid is ~p~n",[User#user.pid]).
Running this code I get:
** exception error: {badrecord,user}
But if I do:
io:format("User ~p~n",[User]).
It prints
User [{user,<0.33.0>,name1,nick1}]
Can anyone point out what i am missing?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是
lists:filter
返回另一个列表,不是单个元素。所以你基本上是试图将列表视为记录。如果仔细查看输出,您会发现该语句被包裹在
[]
中。这是一个清单。如果您只需要第一个用户使用如果您只想要 pid 使用
lists:map< /code>
:
现在
Pids
列出了具有 pid 的用户的 pid。The problems is that
lists:filter
returns another list, not a single element. So you are basically trying to treat a list as a record. If you look carefully in the output ofyou will notice that the statement is wrapped in
[]
. It's a list. If you need just the first user useIf you only want the pids use
lists:map
:Now
Pids
is list with the pids of the users with pid.Emil 关于
lists:filter
函数的回答是正确的。不过,这就是我重写代码的方式:
我假设你可以有多个 pid。如果不这样做,您可以节省自己的 foreach 。
我相信在这种情况下使用 列表推导式 会使代码更具可读性。另外,以下内容:
对我来说看起来不太有用......
Emil's answer about
the lists:filter
function is correct.This is how I would rewrite your code, though:
I'm assuming you can have multiple pids. If you don't, you can save yourself the foreach.
I believe that using list comprehensions in this case makes the code much more readable. Also, the following:
doesn't look very useful to me...
正如其他人指出的那样,lists:filter/2 返回一个列表,即使它只是一个元素。您要查找的函数是
lists:keyfind/3
(在 Erlang R14B03 中,对于 R13B04 及更早版本,请使用lists:keysearch/3
):lists: keyfind/3
是首选,因为它更简单。仅使用
#user.pid
返回字段pid
在#user
记录中的位置:As others have pointed out,
lists:filter/2
returns a list, even if it's just a single element. The function you're looking for islists:keyfind/3
(in Erlang R14B03, for R13B04 and earlier, uselists:keysearch/3
):lists:keyfind/3
is preferred because it's simpler.Using only
#user.pid
returns the position of the fieldpid
in the#user
record:您尝试显示的变量不是一条记录,而是一个其中包含一个元素的列表。该列表中的元素是您想要的记录。考虑与您的
lists:filter
in case 语句的结果进行模式匹配,如下所示:另外,请记住
if 语句
的true
子句。the variable you are trying to display is not a record but rather, a List with one element inside it. The element inside this list is the record you want. Consider pattern matching the result of your
lists:filter
in case statement like this:Also, remember the
true
clause of theif statement
.