Erlang Keyfind没找到

时间:2017-03-18 17:38:31

标签: erlang

我有以下结论:

[tupples, {timeline, [{PID, [stuff]}] }]

我想获得一些东西

parse_data(Data,Pid) ->
case lists:keyfind(timeline, 1, Data) of
    false -> [];
    [] -> [];
    {timeline, Timelines } ->
        io:fwrite("~62p | ~62p ~n", [Pid, Timelines]),
        case lists:keyfind(Pid, 1, Timelines) of
            false ->
                [we end up here];
            RawData -> RawData %% we want to be here
        end
end.

我不知道为什么它会进入第二个假分支。和Pid相匹配。

示例数据:

<13608.329.0> | [{<13608.329.0>,
              [{{'$gen_call',
                    {<13608.847.0>,#Ref<13608.0.2.4361>},
                    get_color},
                #{'__struct__' => 'Elixir.EveryColor.GeneratorWorker.RangeSet',
                  cache => [3288336,3288337,3288338,3288339,
                   3288340,3288341,3288342,3288343],
                  range => #{'__struct__' => 'Elixir.Range',
                    first => 3288344,
                    last => 3321887}}},
               {{'$gen_call',
                    {<13608.847.0>,#Ref<13608.0.2.4175>},
                    get_color},
                #{'__struct__' => 'Elixir.EveryColor.GeneratorWorker.RangeSet',
                  cache => [3288335,3288336,3288337,3288338,
                   3288339,3288340,3288341,3288342,
                   3288343],
                  range => #{'__struct__' => 'Elixir.Range',
                    first => 3288344,
                    last => 3321887}}}]}]

https://github.com/Hajto/erlangpl/blob/master/apps/epl/src/epl_timeline_observer.erl#L67

编辑:

我做了很少的清理工作:

parse_data(Data,Pid) ->
    ListOfProcessHistories = strip_to_timeline(Data),
    strip_to_raw_data(ListOfProcessHistories, Pid).

strip_to_timeline(Data) ->
    case lists:keyfind(timeline, 1, Data) of
        false -> [];
        [] -> [];
        {timeline, Timelines } -> Timelines
    end.

strip_to_raw_data(Data, Pid) ->
    io:fwrite("~62p | ~62p ~n", [Pid, Data]),
    [{Key, _}] = Data,
    io:fwrite("~p || ~p ~n",[Key, Pid]),
    io:fwrite("~p~n", [is_pid(Pid)]),
    io:fwrite("~p~n", [is_pid(Key)]),
    io:fwrite("~p~n",[Pid == Key]),
    case lists:keyfind(Pid, 1, Data) of
        false -> ["kurwa"];
        RawData -> RawData
    end.

执行后:

<13610.708.0> | [{<13610.708.0>,[]}]
<13610.708.0> || <13610.708.0>
true
true
false

我现在被迷住了O_o。

编辑:

term_to_binary/1

与上述方式相同

    <<131,103,100,0,14,116,101,115,116,64,49,50,55,46,48,46,48,46,49,0,0,2,196,0,0,
  0,0,2>> || <<131,103,100,0,14,116,101,115,116,64,49,50,55,46,48,46,48,46,49,
               0,0,2,196,0,0,0,0,0>>

1 个答案:

答案 0 :(得分:2)

Pids有一个名为creation的隐形部分:

http://erlang.org/pipermail/erlang-questions/2014-September/081178.html

如果节点重新启动,这个不可见的部分可以改变,这实际上是一件好事,因为现在可能完全是其他Pid。它们在印刷时显示为相同的东西是有问题的。 要查看它们实际上是不同的,您可以使用term_to_binary并检查最后一个字节。

本地节点上的创建始终为0,远程pid时创建为1,2或3。当您在节点之间发送pid时,它们会自动翻译,但如果您将它们存储为字符串,然后使用list_to_pid将其构建回来,那么创建将为0并且pids不会进行比较。