Search code examples
erlangerlang-otperlang-shell

Property List get_value undefined erlang


I am new to erlang. I have property list as follows.

[{name,<<"127.0.0.1:53290 -> 127.0.0.1:1883">>},{pid,<0.987.0>},{user,<<"guest">>},{type,direct}]

This is my function.

close_single_connection(Conn, ReqData) ->
     io:format("~p\n", Conn),
     case proplists:get_value(pid, Conn) of
          undefined -> io:format("Undefined~n"),ok;
          Pid when is_pid(Pid) ->
              io:format(Pid),
              force_close_connection(ReqData, Conn, Pid)
         end.

io:format("~p\n", Conn) return my property list correctly. But proplists:get_value(pid, Conn) return undefined.

When I am executing following in erlang shell it return Pid value correctly.

List = [{name,<<"127.0.0.1:53290 -> 127.0.0.1:1883">>},{pid,<0.987.0>},{user,<<"guest">>},{type,direct}],
proplists:get_value(pid, List).

Please give me a way to identify the reason.


Solution

  • Issue is in list creation step.

    List should create as follows. This is working properly.

    List = [],
    
    List ++ [{name,<<"127.0.0.1:53290 -> 127.0.0.1:1883">>},{pid,<0.987.0>},{user,<<"guest">>},{type,direct}].
    

    Following is the wrong one. I added redundant [] like this.

    List = [],
    
    X = [{name,<<"127.0.0.1:53290 -> 127.0.0.1:1883">>},{pid,<0.987.0>},{user,<<"guest">>},{type,direct}],
    List ++ [X].