ERLANG - 模式匹配未知大小列表中的特定模式
好吧,现在我想我变暖了,我必须对任何进来的东西进行模式匹配。
所以如果我说
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过以下方式提取数据:
另一种方式:
另一种方式作为模块中的函数(可能是最快的):
You can extract your data this way:
Another way:
And another one as function in module (probably fastest):