问题描述
这是我的第一篇文章,希望大家都很好。所以我刚刚开始erlang,我碰到一个问题,我不知道如何解决。所以我有一个二进制文件,我以
<< 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>
-or -
[[56,23,67,34,45,78],[54,67,87,45,53],[34,56,78]]
01是分隔标签,不需要包含在最终输出中。
$ b $我已经尝试过这样的事情:(如果有更好的方法,请忽略)
parse1([]) - > [];
parse1(1) - > IO:格式( SOHSOHSOHSOHSOHSSOHSOHS);
parse1(回复) - > parse1({Reply,[]});
parse1({Reply,nxtParse}) - >
[H | T] = Reply,
case H of
_当H> 1 - >
[H | nxtParse],
io:format(Reply 1 =〜p〜n,[H]),
parse1({T,nxtParse});
_当H == 1 - >
io:format(SOHSOHSOHSOHSOHSSOHSOHS);
[] - >
ok
结束。
这根本不是很干净,并不像所有的亲的写作类似。当有人告诉我的时候,肯定不了我的头duh。
我意识到绝对有多个解决方案,但最好的是什么。看来ERL有这么多的BIF和做事情的方式,只能找到我的方式,我猜。
感谢您的帮助
-B
随着R14A,Erlang现在包括一个模块来处理这些任务:
1> Bin =< 56,23,67,34,45,78,01,54,67,87,45,53,01,34,56,78> ;.
2>二进制:split(Bin,< 01>,[global])。
[< 56,23,67,34,45,78>,<6CW-5>,<\8N> >]
请注意,虽然看起来不正确(<< 6CW-5>
和<\8N>>
),底层表示是正确的并且字符串是VM试图找出如何打印出二进制文件。以原始格式输出时看到相同的电话:
3> io:format(〜w〜n,[binary:split(Bin,< 01>,[global])])。
[<< 56,23,67,34,45,78><< 54,67,87,45,53>>,< 34,56,78> ;>]
ok
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
Coming with R14A, Erlang now includes a binary
module to handle such tasks:
1> Bin = <<56, 23, 67, 34, 45, 78, 01, 54, 67, 87, 45, 53, 01, 34, 56, 78>>.
<<56,23,67,34,45,78,1,54,67,87,45,53,1,34,56,78>>
2> binary:split(Bin, <<01>>, [global]).
[<<56,23,67,34,45,78>>,<<"6CW-5">>,<<"\"8N">>]
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:
3> io:format("~w~n", [binary:split(Bin, <<01>>, [global])]).
[<<56,23,67,34,45,78>>,<<54,67,87,45,53>>,<<34,56,78>>]
ok
这篇关于ERLANG - 将列表拆分为子列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!