Erlang 模式匹配问题
所以我正在编写一个 Erlang 程序,并且我有一条消息作为字符串通过套接字传入。
我需要检查以确保消息的格式为:[Integer, Space, Integer, "\r\n"] 例如“1 3\r\n”,然后仅当消息与此格式匹配时才执行某些操作。
我已经尝试过,
case Move of
[X1, 32 ,Y1,13,10]->
%do stuff....
true->
%don't do stuff...
end
如果消息正确,它工作正常,但如果消息不匹配,它似乎会崩溃。
我有一种感觉,我可能会以完全错误的方式解决这个问题,但我不确定还能尝试什么...
为任何帮助或建议干杯 =]
编辑:好吧没关系!将 "true->"
替换为 "_->"
可以正常工作 -_- 愚蠢的我!
我仍然有兴趣知道这是否是解决此问题的最佳方法,或者是否有更好的方法。
再次干杯:)
So i'm writing an Erlang program, and I have a message as a string coming in via a socket.
I need to check to make sure the message is in the format: [Integer, Space, Integer, "\r\n"]
e.g. "1 3\r\n" and then only do something if the message matches this format.
I have tried
case Move of
[X1, 32 ,Y1,13,10]->
%do stuff....
true->
%don't do stuff...
end
It works fine if the message is correct, but it just seems to crash if the message doesn't match.
I have a feeling I may be going about this completely the wrong way, but am not sure what else to try...
Cheers for any help or advice =]
EDIT: Ok nevermind! Replacing the "true->"
with "_->"
makes it work just fine -_- silly me!
I'd still be interested to know if this is the best way of going about this, or if there is a better way.
Cheers again :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是 true 你必须使用 _ - 通配符来匹配任何
内容哎呀,看到你的编辑太晚了。
第二个问题的答案是 - 使用函数而不是案例:
instead of true you have to use _ - wildcard which matches anything
P.S. oops, saw your edit too late.
answer to your second question would be - use functions instead of cases: