[erlang-questions] Newbie help please with variable unsafe in 'receive' & variable unused

Vlad Dumitrescu vladdu55@REDACTED
Thu Sep 29 20:53:31 CEST 2016


Hi Donald,

I see. If you need to do processing that involves all three sets of
coordinates, you can't do it like that because after each receive you can
only know one set, the one just received. You have to keep the coords in
 the server state. This is a simple alternative (sorry for the short
names).

% State is [{Orbiter1, {X1, Y1, Z1}}, ...]
orbiter(State) ->
    receive
        {O, C} ->
            State1 = updateState(State, {O, C}),
            process_state(State1),
            orbiter(State1);
        _ ->
           % something
           ok
    end.

This is quite standard simple server, I suggest you check a good
introduction to that. http://learnyousomeerlang.com/ is my favorite.

regards,
Vlad


On Thu, Sep 29, 2016 at 6:29 PM, Donald Steven <t6sn7gt@REDACTED> wrote:

> Thanks Vlad, that works.  However, there are really 3 orbiters and I need
> to use the values of X1, Y1, Z1, X2, Y2, Z2, and X3, Y3 and Z3 outside of
> the 'receive', to compare them, calculate distances between points, etc.
>
> When I try:
>
> loop(Orbiter1, Orbiter2, Orbiter3) ->
>
>     receive
>         {Orbiter1, Coordinates = {{X1, Y1, Z1}}} ->                 % the
> io:format statements are just fro debugging.  What I really need
>             io:format("Coordinates: ~p~n", [Coordinates]),            %
> are the X1, Y1, and Z1 coordinates so that I can do some work with them
>             io:format("X1: ~p, Y1: ~p, Z1: ~p~n", [X1, Y1, Z1]);    %
> beyond the end of the 'receive'
>         {Orbiter2, Coordinates = {{X2, Y2, Z2}}} ->
>             io:format("Coordinates: ~p~n", [Coordinates]),
>             io:format("X2: ~p, Y2: ~p, Z2: ~p~n", [X2, Y2, Z2]);
>         {Orbiter3, Coordinates = {{X3, Y3, Z3}}} ->
>             io:format("Coordinates: ~p~n", [Coordinates]),
>             io:format("X3: ~p, Y3: ~p, Z3: ~p~n", [X3, Y3, Z3]);
>         _ ->
>             ok % stop if unknown message arrives
>     end,
>
>     io:format("X1: ~p, Y1: ~p, Z1: ~p~n", [X1, Y1, Z1]),        % this
> would really be more substantial code.  This is a placeholder
>     loop(Orbiter1, Orbiter2, Orbiter3).
>
> =================
>
> I get:
>
> problem.erl:48: variable 'X1' unsafe in 'receive' (line 34)
> problem.erl:48: variable 'Y1' unsafe in 'receive' (line 34)
> problem.erl:48: variable 'Z1' unsafe in 'receive' (line 34)
> {"init terminating in do_boot",{undef,[{problem,
> main,[],[]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
> init terminating in do_boot ()
>
> =================
>
> but if I move the 'loop(Orbiter1, Orbiter2, Orbiter3)' statement back
> within the clauses in 'receive', it never reaches the code beyond 'end,'.
>
> &^(**(^(*(&\&%$^&%$%^$!!!!
>
> In everyday English, I want to:
>
> 1.  Get the X, Y and Z coordinates from each Orbiter (the concurrent
> processes that are producing them (verified correctly))
> 2.  Do some work with the coordinates (e.g., calculating how far apart the
> orbiters are)
>
> Don
>
> On 09/29/2016 11:02 AM, Vlad Dumitrescu wrote:
>
> loop(Orbiter1) ->
>     receive
>         {Orbiter1, Coordinates={{X,Y,Z}}} ->
>             io:format("Coordinates: ~p~n", [Coordinates]),
>             io:format("X: ~p, Y: ~p, Z: ~p~n", [X, Y, Z]),
>             loop(Orbiter1);
>         _ ->
>             ok % stop if unknown message arrives
>     end.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160929/23635f69/attachment.htm>


More information about the erlang-questions mailing list