test with loop in erlang

2019-08-26 16:31发布

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

标签: erlang
2条回答
放我归山
2楼-- · 2019-08-26 16:47

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]
查看更多
Anthone
3楼-- · 2019-08-26 16:55

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 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 says {313,[]}. In order to get the value of the number bound to your Formvariable, you need to decompose the tuple, which is typically done by pattern matching:

{Form,_Rest}=string:to_integer(FormId)

Now your Form will contain only the number 313.

The string:to_integerfunction will also happily eat the leading zeroes:

1> {Form, _} = string:to_integer("000010"), Form.
10
查看更多
登录 后发表回答