<div dir="ltr">Hi,<div><br></div><div>Your receive for interact function is trying to match Pid along with Response:</div><div><br></div><div><br style="font-size:12.8px"><span style="font-size:12.8px">interact(Pid, Request) -></span><br style="font-size:12.8px"><span style="font-size:12.8px"> Pid ! {self(), Request},</span><br style="font-size:12.8px"><span style="font-size:12.8px"> receive</span><br style="font-size:12.8px"><span style="font-size:12.8px"> <b>{Pid, Response} -> Response</b></span><br style="font-size:12.8px"><span style="font-size:12.8px"> end.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">loop(Table) -></span><br style="font-size:12.8px"><span style="font-size:12.8px"> receive</span><br style="font-size:12.8px"><span style="font-size:12.8px"> {From, {insert, key, value}} -></span><br style="font-size:12.8px"><span style="font-size:12.8px"> From ! ets:insert(Table, {Key, Value}), %% << you are not sending something to match with Pid</span><br style="font-size:12.8px"><span style="font-size:12.8px"> loop(Table);</span><br style="font-size:12.8px"><span style="font-size:12.8px"> {From, {lookup, key}} -></span><br style="font-size:12.8px"><span style="font-size:12.8px"> From ! ets:lookup(Table, Key), </span><span style="font-size:12.8px">%% << you are not sending something to match with Pid</span><br style="font-size:12.8px"><span style="font-size:12.8px"> loop(Table);</span><br style="font-size:12.8px"><span style="font-size:12.8px"> Any -></span><br style="font-size:12.8px"><span style="font-size:12.8px"> Any</span><br style="font-size:12.8px"><span style="font-size:12.8px"> end.</span><br></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">So I think this correct send code should be: </span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">From ! {<b>self(),</b> ets:insert(Table, {Key, Value})}</span><br></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">and</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">From ! {<b>self(), </b>ets:lookup(Table, Key)},</span><br></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Thanks,</span></div><div><span style="font-size:12.8px">Pravin</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 15, 2016 at 1:28 PM, Pietro <span dir="ltr"><<a href="mailto:pulsarpietro@posteo.net" target="_blank">pulsarpietro@posteo.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><a href="mailto:dmkolesnikov@gmail.com">dmkolesnikov@gmail.com</a> writes:<br>
<br>
> Hello,<br>
><br>
> You are using atoms instead of variables in your program. Erlang<br>
> variables starts with capital letter.<br>
><br>
> The receive loop cannot match your message:<br>
><br>
> {From, {insert, key, value}} -><br>
><br>
> It expects one triple of atoms but you send different one.<br>
><br>
> test:interact(Pid, {insert, testkey, testvalue}).<br>
><br>
> Dmitry<br>
><br>
> Sent from my iPhone<br>
</span>Ouch ! Thank you very much, I knew I were missing something crucial<br>
here, would you help me in my next step ?<br>
<br>
loop(Table) -><br>
receive<br>
{From, {insert, Key, Value}} -><br>
io:format("Here insert"),<br>
Return = ets:insert(Table, {Key, Value}),<br>
From ! Return,<br>
loop(Table);<br>
{From, {lookup, Key}} -><br>
io:format("Here lookup"),<br>
Return = ets:lookup(Table, Key),<br>
From ! Return,<br>
loop(Table);<br>
{From, Any} -><br>
From ! {self(), {error,Any}},<br>
loop(Table)<br>
end.<br>
<br>
This code results in :<br>
<br>
1> c(test).<br>
{ok,test}<br>
2> Pid = test:start().<br>
<0.40.0><br>
3> Pid ! {self(), {insert, a, 1}}.<br>
Here insert{<0.33.0>,{insert,a,1}}<br>
4> Pid ! {self(), {lookup, a}}.<br>
Here lookup{<0.33.0>,{lookup,a}}<br>
<br>
<br>
It does not return the the stored values even though it clearly matches<br>
the patterns, where is the rub here ?<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
><br>
> On 15 Mar 2016, at 00:53, Pietro <<a href="mailto:pulsarpietro@posteo.net">pulsarpietro@posteo.net</a>> wrote:<br>
><br>
> Hi all,<br>
><br>
> I have recently started to implement a small academic project in<br>
> Erlang<br>
> and I have bumped into a problem which seems to show to myself I<br>
> haven't<br>
> grasped something important about the technology itself.<br>
><br>
> That's my code :<br>
><br>
> -module(test).<br>
> -export([start/0, interact/2]).<br>
><br>
><br>
> start() -><br>
> spawn (fun() -> startloop() end).<br>
><br>
><br>
> interact(Pid, Request) -><br>
> Pid ! {self(), Request},<br>
> receive<br>
> {Pid, Response} -> Response<br>
> end.<br>
><br>
><br>
> startloop() -><br>
> TableId = ets:new(dictionary, [set]),<br>
> loop(TableId).<br>
><br>
> loop(Table) -><br>
> receive<br>
> {From, {insert, key, value}} -><br>
> From ! ets:insert(Table, {key, value}),<br>
> loop(Table);<br>
> {From, {lookup, key}} -><br>
> From ! ets:lookup(Table, key),<br>
> loop(Table);<br>
> Any -><br>
> Any<br>
><br>
> end.<br>
><br>
><br>
> The problem happens when I try to interact with the server I start<br>
> using<br>
> the command:<br>
><br>
> Pid = test:start().<br>
><br>
> Then I run :<br>
><br>
> test:interact(Pid, {insert, testkey, testvalue}).<br>
><br>
> My ershell at this point hangs and nothing happens ... what is<br>
> happening<br>
> ? Please feel free to redirect me to a more appropriate newsgroup<br>
> if my<br>
> question is not appropriate or if there is a better one.<br>
><br>
> Thanks in advance.<br>
><br>
> Pietro.<br>
><br>
> _______________________________________________<br>
> erlang-questions mailing list<br>
> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
> <a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
><br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</div></div></blockquote></div><br></div>