ERLANG - 将列表拆分为子列表
嗨,这是我在这里的第一篇文章,希望你们一切都好。所以我刚刚开始 erlang,我遇到了一个问题,我还不知道如何解决。
所以我有一个以 01 的形式收到的二进制文件。
<<56, 23, 67, 34, 45, 78, 01, 54, 67, 87, 45, 53, 01, 34, 56, 78>>
我的目标是将其拆分为基于 01 的子列表(如果更有效,则为二进制列表)。
例如,上面的内容应该如下所示:
<<56, 23, 67, 34, 45, 78>> <<54, 67, 87, 45, 53>> <<34, 56, 78>>
- 或 -
[[56, 23, 67, 34, 45, 78], [54, 67, 87, 45, 53], [34, 56, 78]]
01 是分隔标签,它不需要包含在最终输出中。
我已经尝试过这样的事情:(如果有更好的方法,请忽略)
parse1([]) -> [];
parse1(1) -> io:format("SOHSOHSOHSOHSOHSSOHSOHS");
parse1(Reply) -> parse1({Reply, []});
parse1({Reply, nxtParse}) ->
[H | T] = Reply,
case H of
_ when H > 1 ->
[H | nxtParse],
io:format("Reply 1 = ~p~n", [H]),
parse1({T, nxtParse});
_ when H == 1 ->
io:format("SOHSOHSOHSOHSOHSSOHSOHS");
[] ->
ok
end.
这根本不是很干净,而且根本不像专业人士写的那样。我确信当有人向我提供线索时,我会“呃”地拍打我的头。
我意识到肯定有不止一种解决方案,但最好的是什么。看来 ERL 有这么多 BIF 和做事方式,我想我只能找到我的方法了。
感谢大家的帮助 -B
Hi this is my first post here hope you all are well. So Im just starting erlang and I ran into a problem im not sure how to tackle yet.
So I have a binary I am recieving in the form of
<<56, 23, 67, 34, 45, 78, 01, 54, 67, 87, 45, 53, 01, 34, 56, 78>>
My goal is to split it into a sub list (or binary if more efficient) based on the 01.
For example the above should come out looking like:
<<56, 23, 67, 34, 45, 78>> <<54, 67, 87, 45, 53>> <<34, 56, 78>>
-or-
[[56, 23, 67, 34, 45, 78], [54, 67, 87, 45, 53], [34, 56, 78]]
The 01 is the separating tag, it does not need to be included in the final output.
I have tried something as such: (PLEASE disregard if there is a better way)
parse1([]) -> [];
parse1(1) -> io:format("SOHSOHSOHSOHSOHSSOHSOHS");
parse1(Reply) -> parse1({Reply, []});
parse1({Reply, nxtParse}) ->
[H | T] = Reply,
case H of
_ when H > 1 ->
[H | nxtParse],
io:format("Reply 1 = ~p~n", [H]),
parse1({T, nxtParse});
_ when H == 1 ->
io:format("SOHSOHSOHSOHSOHSSOHSOHS");
[] ->
ok
end.
This isn't really clean at all and doesn't resemble at all what pro's write. Im sure Ill smack my head "duh" when someone clues me in.
I realize there is definitely more than one solution, but what is the BEST one. It seems ERL has so many BIF's and way to do things, just gotta find my way around I guess.
Thanks for the help guys
-B
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与 R14A 一起,Erlang 现在包含一个
binary
模块来处理此类任务:请注意,尽管它看起来是错误的(
<<"6CW-5">>
和<<"\"8N">>
>),底层表示是正确的,字符串是虚拟机试图弄清楚如何打印出二进制文件,以原始格式输出时请参阅相同的调用:Coming with R14A, Erlang now includes a
binary
module to handle such tasks:Note that although it looks wrong (
<<"6CW-5">>
and<<"\"8N">>
), the underlying representation is right and the strings are the VM trying to figure out how to print out the binaries. See the same call when outputting in a raw format: