我需要用erlang编写一个if语句,条件是在我接收的元组中搜索此测试“ tele / folin-house-s1 / SENSOR”。 在下面插入要接收的元组:
{message,<<0,5,128,208,182,250,162,9,244,64,0,0,105,198,0,0>>,
1,<<"DVES_0DBFDB">>,
#{dup => false,retain => true},
#{username => undefined},
<<"tele/folin-house-s1/LWT">>,<<"Offline">>,
{1549,8794,985040}}.
{message,<<0,5,128,208,182,250,162,9,244,64,0,0,105,198,0,0>>,
1,<<"DVES_0DBFDB">>,
#{dup => false,retain => true},
#{username => undefined},
<<"tele/folin-house-s1/LWT">>,<<"Offline">>,
{1549,8794,985040}}.
{message,undefined,0,<<"733f0bfd-0741-4e55-bd14-ba10a156165f1549007325254">>,
#{dup => false,retain => false},
#{username => undefined},
<<"tele/folin-house-s1/SENSOR">>,
<<"{\"Time\":\"2018-12-18T22:37:00\",\"BME680\":{\"Temperature\":21.3,\"Humidity\":38.1,\"Pressure\":1021.4,\"Gas\":308.19},\"TempUnit\":\"C\"}">>,
{1549,8796,895335}}.
{message,undefined,0,<<"733f0bfd-0741-4e55-bd14-ba10a156165f1549007325254">>,
#{dup => false,retain => false},
#{username => undefined},
<<"tele/folin-house-s1/SENSOR">>,
<<"{\"Time\":\"2018-12-18T22:37:00\",\"BME680\":{\"Temperature\":21.3,\"Humidity\":38.1,\"Pressure\":1021.4,\"Gas\":308.19},\"TempUnit\":\"C\"}">>,
{1549,8803,302986}}.
我将使用if语句仅过滤这样的元组:
{message,undefined,0,<<"733f0bfd-0741-4e55-bd14-ba10a156165f1549007325254">>,
#{dup => false,retain => false},
#{username => undefined},
<<"tele/folin-house-s1/SENSOR">>,
<<"{\"Time\":\"2018-12-18T22:37:00\",\"BME680\":{\"Temperature\":21.3,\"Humidity\":38.1,\"Pressure\":1021.4,\"Gas\":308.19},\"TempUnit\":\"C\"}">>,
{1549,8803,302986}}.
包含“ tele / folin-house-s1 / SENSOR”的。我不知道erlang编码。
答案 0 :(得分:2)
可能最好使用case子句
在这种情况下,您需要通过函数传递接收到的元组
这是函数示例:
message({message,_, _,_, _, _, <<"tele/folin-house-s1/LWT">>,_, _}) ->
do_something();
message(_Tuple) -> skip.
这是带有'case'语句的示例:
message({message,_, _,_, _, _, Condition,_, _}) ->
case Condition of
<<"tele/folin-house-s1/LWT">> -> do_something();
_Any -> skip
end.
下面是带有'if'语句的示例:
message({message,_, _,_, _, _, Condition,_, _}) ->
if
Condition =:= <<"tele/folin-house-s1/LWT">> ->
do_something();
true ->
skip
end.