Erlang列表过滤问题

发布于 2024-10-17 05:07:22 字数 738 浏览 2 评论 0原文

我有一个记录,其中一个字段是列表:

-record(state, {
        rcpt :: list()
            }).

Rcpt 字段有一些值 =

[“本地主机”,“管理员”]

我需要过滤此列表。例如我需要字段 localhost。

我尝试:

List = lists:filter(fun(X) -> LocalHost =:= X end, State#state.rcpt),

但列表是空的。

我做错了什么?

我不明白。看:

io:format(State#state.rcpt),
    %% output: localhost
HaveRcpt = lists:member("localhost", State#state.rcpt ),
io:format(HaveRcpt);
    %% output false

但是如果我使用:

io:format(State#state.rcpt),
HaveRcpt = lists:member("localhost", lists:nth(1,State#state.rcpt) ),
io:format(HaveRcpt);
%% true

谢谢。

I have record in which one field is list:

-record(state, {
        rcpt :: list()
            }).

Rcpt field has some values =

["localhost", "admin"]

I need filter this list. For example i need field localhost.

I try:

List = lists:filter(fun(X) -> LocalHost =:= X end, State#state.rcpt),

but List is empty.

What's wrong i do?

I don't understand it. Look:

io:format(State#state.rcpt),
    %% output: localhost
HaveRcpt = lists:member("localhost", State#state.rcpt ),
io:format(HaveRcpt);
    %% output false

But if i use:

io:format(State#state.rcpt),
HaveRcpt = lists:member("localhost", lists:nth(1,State#state.rcpt) ),
io:format(HaveRcpt);
%% true

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吹梦到西洲 2024-10-24 05:07:22

如果您已经拥有完整的值,那么您的真正目标不是要查看该值是否只是列表中的成员吗?

HaveRcpt = lists:member( "localhost", State#state.rcpt ) 
% HaveRcpt will have value true of false

如果该

case lists:member( "admin", State#state.rcpt ) of 
 true -> is_admin;
 false -> is_no_admin
end

值可能因任何原因重复多次并且您想要所有这些字段,则可以使用类似这样的列表:filter/2。

lists:filter(fun(X) -> "localhost"==X end, State#state.rcpt)
or
[ X || X <- State#state.rcpt, X=="localhost" ]

您在示例中引入了变量 LocalHost。这必须是与您的元素(“localhost”、“admin”)完全相同的值,才能返回空列表以外的任何内容。

If you already have the full value isn't your real objective to see if that value is just a member of the list or not?

HaveRcpt = lists:member( "localhost", State#state.rcpt ) 
% HaveRcpt will have value true of false

That may be used like

case lists:member( "admin", State#state.rcpt ) of 
 true -> is_admin;
 false -> is_no_admin
end

If the value may be repeated several times for any reasons and you want all of those fields, you may use the lists:filter/2 like this.

lists:filter(fun(X) -> "localhost"==X end, State#state.rcpt)
or
[ X || X <- State#state.rcpt, X=="localhost" ]

You introduced a variable LocalHost in your example. This must be the exact value as your element ("localhost","admin") to ever return anything else then a empty list.

莫多说 2024-10-24 05:07:22

首先,最好为 rcpt 字段设置默认值:

-record(state, {
    rcpt = [] :: [string()]
    }).

然后需要正确设置 rcpt 字段值(最好将此操作封装在设置函数中):

 S = #state{rcpt=["localhost", "admin"]}

然后您可以按预期使用 lists:member 的所有内容:

true = lists:member("localhost", S#state.rcpt),
true = lists:member("admin", S#state.rcpt),
false = lists:member("other", S#state.rcpt)

Firstly, it's better to set default value for rcpt field:

-record(state, {
    rcpt = [] :: [string()]
    }).

Then you need to set rcpt field value correctly (it's better to encapsulate this operation in a setup function):

 S = #state{rcpt=["localhost", "admin"]}

And after all that you can use lists:member as expected:

true = lists:member("localhost", S#state.rcpt),
true = lists:member("admin", S#state.rcpt),
false = lists:member("other", S#state.rcpt)
谷夏 2024-10-24 05:07:22

如果

HaveRcpt = lists:member("localhost", lists:nth(1,State#state.rcpt) ),
io:format(HaveRcpt).
%% true

您的 State#state.rcpt 必须包含 [["localhost"]|_]

1> lists:member("localhost", lists:nth(1,["localhost","admin"]) ).
false
2> lists:member("localhost", lists:nth(1,[["localhost"],"admin"]) ).
true

如果您有正确的内容,它将按预期工作

3> L = ["localhost","admin"].                                                                                                                                                                        
["localhost","admin"]
4> lists:filter(fun(X) -> "localhost" =:= X end, L).
["localhost"]
5> [X || X<-L, "localhost" =:= X].                                                                                                                                                                   
["localhost"]

If

HaveRcpt = lists:member("localhost", lists:nth(1,State#state.rcpt) ),
io:format(HaveRcpt).
%% true

then your State#state.rcpt have to contain [["localhost"]|_]

1> lists:member("localhost", lists:nth(1,["localhost","admin"]) ).
false
2> lists:member("localhost", lists:nth(1,[["localhost"],"admin"]) ).
true

If you would have correct content it would work as expected

3> L = ["localhost","admin"].                                                                                                                                                                        
["localhost","admin"]
4> lists:filter(fun(X) -> "localhost" =:= X end, L).
["localhost"]
5> [X || X<-L, "localhost" =:= X].                                                                                                                                                                   
["localhost"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文