tags:

views:

54

answers:

1

I have the following code

loop(Data) ->
receive
    {Key, Value} ->
    [{Key, Value}|Data];
    {Key} ->
        member(Key, Data);
14  loop(Data);
    stop ->
        io:format("server_stopped"),
        ok  
end .

and I get the following error (I put the line number 14 in the code)

./dist_erlang.erl:14: syntax error before: ';' ./dist_erlang.erl:2: function loop/1 undefined ./dist_erlang.erl:28: Warning: function member/2 is unused

I am not sure what the syntax problem is with the above code. I have a method called member which is giving the error because of a different syntax error on line 14 I am sure.

Any help is appreciated thanks.

+3  A: 

In Erlang, expressions are separated by commas (and clauses are separated by semicolons). Try:

loop(Data) -> 
    receive 
        {Key, Value} -> 
            loop([{Key, Value}|Data]); 
        {Key} -> 
            member(Key, Data),
            loop(Data);
        stop -> 
            io:format("server_stopped"), 
            ok   
    end.
Greg Hewgill
Does this mean I want to loop after receiving {Key}? I want to loop , which keeps this process running, after all clauses have been evaluated.
alJaree
Oh, I misunderstood what your code meant. I've revised the example and placed the `loop(Data)` outside the `receive` block.
Greg Hewgill
Ok thanks, will receiving stop end the process? I only have a print statement, but I dont think that ok. stops this process or does it? thanks
alJaree
Actually, on second look your code appears to have more than just that problem. I've revised my example again to show looping on the two first messages and to exit on `stop`.
Greg Hewgill