Test with a loop in erlang
I have a list of values ββ"Z0010", "Z0011", "Z0012", "Z0013" "Z0014", "Z0015", "Z0016", "Z0017", "Z0018", "Z0019"
I want to create a function that takes a value in a parameter
and I want to do a test in my function if the value passed as a parameter is equal to the value in the list, in which case it will show "existing" if it doesn't display "does not exist"
I'm trying to:
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
Now I am trying to follow 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 , not false
I think Form=string:to_integer(FormId),
it will not return in this case 313
and one more thing I want to add to my code
for example, if H is "Z000010" FormId = string: substr (H, 2, length (H)),
it returns "000010" Now I want to exclude the first zero before the first integer is null, so extarct 0000 to 1
source to share
lists:map/2
takes one list and creates a new list with the same number of values, so your list of 10 values ββis converted to a list of 10 {ok}
or {notok}
tuples.
You probably want to lists:member/2
.
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]
source to share
Have a look at the documentation ( http://www.erlang.org/doc/man/string.html#to_integer-1 ):
to_integer(String) -> {Int, Rest} | {error, Reason}
Types:
String = string()
Int = integer()
Rest = string()
Reason = no_integer | not_a_list
So, it to_integer
returns a tuple containing the number that was consumed from the string and the rest of the string. You can even tell from your test output where it speaks {313,[]}
. To get the value of the number associated with your variable Form
, you need to decompose a tuple, which is usually done by pattern matching:
{Form,_Rest}=string:to_integer(FormId)
Now yours Form
will only contain a number 313
.
The function string:to_integer
also happily eats leading zeros:
1> {Form, _} = string:to_integer("000010"), Form.
10
source to share