Erlang 新手:添加了 IF,变量不起作用?

发布于 2024-11-30 05:27:29 字数 1797 浏览 0 评论 0原文

有谁擅长调试 Erlang 吗?我一生都无法弄清楚出了什么问题。无论我将 Fields 变量放在哪里,Erlang 都会说该行之前有一个错误...

编译消息:

./eventbus.erl:6: syntax error before: FieldPositions
./eventbus.erl:24: variable 'FieldPositions' is unbound
./eventbus.erl:28: Warning: variable 'Ref' is unused
./eventbus.erl:30: Warning: variable 'List' is unused
error

然后是代码本身。

-module(secret).
-export([listen/1, send/1]).

-define(TCP_OPTIONS, [binary, {packet, 0}, {active, false}, {reuseaddr, true}]).

FieldPositions = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","AA","BB","CC","DD","EE","FF","GG","HH","II","JJ","KK","LL","MM","NN","OO","PP"].


listen(Port) ->
    {ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
    accept(LSocket).

accept(LSocket) ->
    {ok, Socket} = gen_tcp:accept(LSocket),
    spawn(fun() -> loop(Socket) end),
    accept(LSocket).    

discrim(<<>>) ->
    ok;
discrim([]) ->
    ok;
discrim(Info) ->
    EventsList = string:tokens(Info,"|"),
    process_events(EventsList, FieldPositions).

process_events([],[]) ->
    ok;
process_events([],Ref) ->
    ok;
process_events(List,[]) ->
    ok;
process_events(List,Ref) ->
    [RHead|RTail] = Ref,
    [Head|Tail] = List,
    if
    Head == [] ->
        process_events(Tail, RTail);
    true ->
        io:format("message.bus ~s ~s",[RHead,Head]),
        process_events(Tail, RTail),
        ok
    end.

loop(Socket) ->
    case gen_tcp:recv(Socket, 0) of
        {ok, Data} ->
            %gen_tcp:send(Socket, Data),
            %io:format(Data),
            discrim(Data),
            io:format("~n"),
            loop(Socket);
        {error, closed} ->
            ok
    end.

Anyone good at debugging Erlang? I can't figure out for the life of me what's wrong. Wherever I put the Fields variable, Erlang says that there's an error before that line...

Compile messages:

./eventbus.erl:6: syntax error before: FieldPositions
./eventbus.erl:24: variable 'FieldPositions' is unbound
./eventbus.erl:28: Warning: variable 'Ref' is unused
./eventbus.erl:30: Warning: variable 'List' is unused
error

And then the code itself.

-module(secret).
-export([listen/1, send/1]).

-define(TCP_OPTIONS, [binary, {packet, 0}, {active, false}, {reuseaddr, true}]).

FieldPositions = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","AA","BB","CC","DD","EE","FF","GG","HH","II","JJ","KK","LL","MM","NN","OO","PP"].


listen(Port) ->
    {ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
    accept(LSocket).

accept(LSocket) ->
    {ok, Socket} = gen_tcp:accept(LSocket),
    spawn(fun() -> loop(Socket) end),
    accept(LSocket).    

discrim(<<>>) ->
    ok;
discrim([]) ->
    ok;
discrim(Info) ->
    EventsList = string:tokens(Info,"|"),
    process_events(EventsList, FieldPositions).

process_events([],[]) ->
    ok;
process_events([],Ref) ->
    ok;
process_events(List,[]) ->
    ok;
process_events(List,Ref) ->
    [RHead|RTail] = Ref,
    [Head|Tail] = List,
    if
    Head == [] ->
        process_events(Tail, RTail);
    true ->
        io:format("message.bus ~s ~s",[RHead,Head]),
        process_events(Tail, RTail),
        ok
    end.

loop(Socket) ->
    case gen_tcp:recv(Socket, 0) of
        {ok, Data} ->
            %gen_tcp:send(Socket, Data),
            %io:format(Data),
            discrim(Data),
            io:format("~n"),
            loop(Socket);
        {error, closed} ->
            ok
    end.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜吻♂芭芘 2024-12-07 05:27:29

您尝试在模块级别声明一个值 FieldPositions,这在 Erlang 中是不允许的。 (但是您可以在模块级别声明函数,或在函数内声明值。)

有许多替代方法可以实现您想要的效果:

  • 使用 -define 宏,非常类似于TCP_OPTIONS 代码中的一个。

  • FieldPositions 的值内联到您想要使用它的位置。

  • 编写一个常量返回函数,例如:field_positions() -> ["A","B",...,"PP"].

顺便说一句,根据我使用各种编译器和解释器的经验,我建议您不要过于字面地理解错误消息。特别是,我认为您应该将收到的错误视为“错误发生在 x 行周围的某处”,而不是字面上的“在 x 行之前”或“在 x 行处”。

You tried to declare a value FieldPositions at the module level, which is not allowed in Erlang. (But you can declare functions at the module level, or declare values within functions.)

There are a number of alternatives to achieve the effect you want:

  • Use a -define macro, much like the one in your code for TCP_OPTIONS.

  • Inline the value of FieldPositions to where you wanted to use it.

  • Write a constant-returning function, like: field_positions() -> ["A","B",...,"PP"].

As an aside, from my experience with various compilers and interpreters, I recommend you not to take the error messages too literally. In particular, I think you should treat the errors you got as "error happened somewhere around line x", not literally as "before line x" or "at line x".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文