ERLANG - 模式匹配未知大小列表中的特定模式

发布于 2024-09-14 03:06:16 字数 786 浏览 5 评论 0原文

好吧,现在我想我变暖了,我必须对任何进来的东西进行模式匹配。

所以如果我说

Message = = [[<<>>], 
 [<<"10">>,<<"171">>],
 [<<"112">>,<<"Gen20267">>],
 [<<"52">>,<<"20100812-06:32:30.687">>]] 

并且我正在寻找模式匹配字段<<“112”>>>

例如 112 总是会说 112,但 Gen2067 可以随时更改为任何内容......它的数据,它将存储在变量中。

此外,字段可以按任何顺序排列,无论我尝试执行什么功能都必须能够找到该字段并解析它。

这是我现在使用的代码:

loop() ->
receive
    [_,[<<"112">>, Data], _] when is_list(X) -> %% Just dosen't work in anyway..
        ?DEBUG("Got a list ~p~n", [X]),
        loop();
    _Other ->
        ?DEBUG("I don't understand ~p~n", [_Other]),
        loop()
end.

我感觉很接近,但不是 100%

-B

Ok now I think Im getting warmer, I have to pattern match whatever comes in.

So if I had say

Message = = [[<<>>], 
 [<<"10">>,<<"171">>],
 [<<"112">>,<<"Gen20267">>],
 [<<"52">>,<<"20100812-06:32:30.687">>]] 

And I was looking to pattern match the field <<"112">>

Such as the 112 is always going to say 112, but the Gen2067 can change whenever to whatever.. its the data, it will be stored in a variable.

Also the fields can be in any order, whatever function im trying to do has to be able to find the field and parse it.

This is the code I am using right now:

loop() ->
receive
    [_,[<<"112">>, Data], _] when is_list(X) -> %% Just dosen't work in anyway..
        ?DEBUG("Got a list ~p~n", [X]),
        loop();
    _Other ->
        ?DEBUG("I don't understand ~p~n", [_Other]),
        loop()
end.

I feel im close, but not 100%

-B

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

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

发布评论

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

评论(1

愿得七秒忆 2024-09-21 03:06:16

您可以通过以下方式提取数据:

1> Message = [[<<>>],                        
1>  [<<"10">>,<<"171">>],                    
1>  [<<"112">>,<<"Gen20267">>],              
1>  [<<"52">>,<<"20100812-06:32:30.687">>]] .
[[<<>>],
 [<<"10">>,<<"171">>],
 [<<"112">>,<<"Gen20267">>],
 [<<"52">>,<<"20100812-06:32:30.687">>]]
2> [Data] = [X || [<<"112">>, X] <- Message ].
[<<"Gen20267">>]
3> Data.
<<"Gen20267">>

另一种方式:

4> [_, Data] = hd(lists:dropwhile(fun([<<"112">>|_]) -> false; (_)->true end, Message)).
[<<"112">>,<<"Gen20267">>]
5> Data.
<<"Gen20267">>

另一种方式作为模块中的函数(可能是最快的):

% take_data(Message) -> Data | not_found
take_data([]) -> not_found;
take_data([[<<"112">>, Data]|_]) -> Data;
take_data([_|T]) -> take_data(T).

You can extract your data this way:

1> Message = [[<<>>],                        
1>  [<<"10">>,<<"171">>],                    
1>  [<<"112">>,<<"Gen20267">>],              
1>  [<<"52">>,<<"20100812-06:32:30.687">>]] .
[[<<>>],
 [<<"10">>,<<"171">>],
 [<<"112">>,<<"Gen20267">>],
 [<<"52">>,<<"20100812-06:32:30.687">>]]
2> [Data] = [X || [<<"112">>, X] <- Message ].
[<<"Gen20267">>]
3> Data.
<<"Gen20267">>

Another way:

4> [_, Data] = hd(lists:dropwhile(fun([<<"112">>|_]) -> false; (_)->true end, Message)).
[<<"112">>,<<"Gen20267">>]
5> Data.
<<"Gen20267">>

And another one as function in module (probably fastest):

% take_data(Message) -> Data | not_found
take_data([]) -> not_found;
take_data([[<<"112">>, Data]|_]) -> Data;
take_data([_|T]) -> take_data(T).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文