测试与循环在erlang [英] test with loop in erlang
问题描述
我想开发一个在参数
中获取值的函数,我想在我的函数如果作为参数传递的值等于列表中的值,在这种情况下会显示existse,如果不显示not existe
我尝试与:
test(Token) - >
case get_user_formid_by_token(令牌)
{ok,H} - >
FormId = string:substr(H,2,length(H)),
Form = string:to_integer(FormId),
案例验证(0019,1,Form)
{ok} - > io:format(existse);
{notok} - > io:format(not existe)
end;
{error,notfound} - > io:format(qq)
end。
verify(VariableLength,VariableIncrement,FormId) - >
列表:map(fun(I) - > if I =:= FormId - >
{ok};
I = / = FormId - >
{notok}
end
end,
lists:seq(0010,VariableLength,VariableIncrement))。
但是当我执行此代码时,它显示:
1>型号:测试( 21137900)。
**异常错误:no case子句匹配[{notok},
{notok},
{notok},
{notok},
{notok}
{notok},
{notok},
{notok},
{notok},
{notok}]
在函数模型中: 1
我现在尝试使用此解决方案:
get_user_formid_by_token(令牌) - >
Q = qlc:q([{X#person.formid} || X< - mnesia:table(person),
X#person.token =:= Token]),
case do(Q)of
[{H}] - >
{ok,H};
[] - >
{error,notfound}
end。
测试(令牌) - >
case get_user_formid_by_token(令牌)
{ok,H} - >
io:format(〜s〜n,[H]),
FormId = string:substr(H,5,length(H)),
io:format s〜n,[FormId]),
Form = string:to_integer(FormId),
io:format(〜p〜n,[Form]),
(表格,列表:seq(313,320,1));
{error,notfound} - > io:format(qq)
end。
但是当我测试我在控制台中有这个消息:
1>型号:测试( 21137900)。
Z000313
313
{313,[]}
false
结果应该是 true 而不是 false
我认为 Form = string:to_integer(FormId),
在这种情况下不返回313
另一件我想添加到我的代码中的代码
,例如 H 等于Z000010 FormId = string:substr(H,2,length(H)) ,
它返回000010
现在我想要消除第一个零之前的第一个整数不为null所以extarct 0000
before 1
列表:map / 2
列出并创建具有相同数量值的新列表,因此您的10个值的列表将转换为10 {ok}
或 {你可能想要
列表:member / 2
而不是
/ p>
5>列表:member(0,lists:seq(1,3,1))。
false
6>列表:member(3,lists:seq(1,3,1))。
true
7>列表:map(fun(X) - > ok end,lists:seq(1,3,1))。
[ok,ok,ok]
I have a list of values "Z0010", "Z0011", "Z0012", "Z0013" "Z0014", "Z0015", "Z0016", "Z0017", "Z0018", "Z0019"
I want to develop a function that takes a value in parameter
and I want to do a test in my function if the value passed as a parameter is equal to a value in the list in this cases it will show "existe" if not it displays "not existe"
I try with :
test(Token)->
case get_user_formid_by_token(Token) of
{ok, H} ->
FormId=string:substr(H, 2, length(H)),
Form=string:to_integer(FormId),
case verify (0019,1,Form) of
{ok}->io:format("existe");
{notok}->io:format("not existe")
end;
{error, notfound}-> io:format("qq")
end.
verify(VariableLength,VariableIncrement,FormId)->
lists:map(fun(I) -> if I =:= FormId ->
{ok};
I =/= FormId ->
{notok}
end
end,
lists:seq(0010, VariableLength, VariableIncrement)).
but when I execute this code it displays :
1> model:test("21137900").
** exception error: no case clause matching [{notok},
{notok},
{notok},
{notok},
{notok},
{notok},
{notok},
{notok},
{notok},
{notok}]
in function model:test/1
I try now with this solution :
get_user_formid_by_token(Token) ->
Q = qlc:q([{X#person.formid} || X <- mnesia:table(person),
X#person.token =:= Token]),
case do(Q) of
[{H}] ->
{ok, H};
[] ->
{error, notfound}
end.
test(Token)->
case get_user_formid_by_token(Token) of
{ok, H} ->
io:format("~s~n",[H]),
FormId=string:substr(H, 5, length(H)),
io:format("~s~n",[FormId]),
Form=string:to_integer(FormId),
io:format("~p~n",[Form]),
lists:member(Form, lists:seq(313, 320, 1));
{error, notfound}-> io:format("qq")
end.
but when I test I have this message in the console:
1> model:test("21137900").
Z000313
313
{313,[]}
false
the result should be true and not false
I think that Form=string:to_integer(FormId),
it not return in this case 313
and another thing I want to add in my code
for example if H equal "Z000010" FormId=string:substr(H, 2, length(H)),
it return "000010" Now I want to eliminate the first zero before the first integer not null so extarct 0000 before 1
lists:map/2
takes one list and creates a new list with the same number of values, so your list of 10 values is transformed into a list of 10 {ok}
or {notok}
tuples.
You probably want lists:member/2
instead.
5> lists:member(0, lists:seq(1, 3, 1)).
false
6> lists:member(3, lists:seq(1, 3, 1)).
true
7> lists:map(fun(X) -> ok end, lists:seq(1, 3, 1)).
[ok,ok,ok]
这篇关于测试与循环在erlang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!