ERlANG - 将列表拆分为子列表

发布于 2024-09-14 15:50:11 字数 1158 浏览 5 评论 0原文

所以我的另一篇文章中的一个好人很快就解决了一个问题。

ERLANG - 将列表拆分为子列表

现在我是 Erlang 的初学者,需要对另一个函数的语法有一点帮助,该函数对我上一篇文章的结果起作用。

例如,我现在有:

   Reply = [<<56,45,34,07,45,67,34>>, <<12,23,56,07,67,67>>,  <<55,23,45,07,89,56>>]

我需要将其进一步拆分为:

[ [<<56,45,34>>,<<45,67,34>>], [<<12,23,56>>,<<67,67>>] ,  [<<55,23,45>>, <<89,56>>]  ]

本例中的分隔符是 <<07>>>

此代码过程是二进制文件

parse(Reply) -> binary:split(Reply, <<07>>, [global]).

,但现在我如何递归地遍历数组并再次执行此操作。

这是我当前代码的一个示例:

parse(Reply) -> binary:split(Reply, <<01>>, [global]).
parse2(Reply) -> binary:split(Reply, <<07>>, [global]).

func1(Done) -> [parse2(X) || X <- Done].

%%blah blah - get to the executing functions code
Done = parse(Reply),
Done1 = func1(Done),

我知道它必须是非常简单的东西,最后一个代码确实让我满意。

最好的, -B

So this I just had one problem solved very quickly by a nice guy on my other post.

ERLANG - Splitting Lists into sub list

Now I am a beginner at Erlang and need a little help with the syntax of another function that does work on the result from my previous post.

For example, I now have:

   Reply = [<<56,45,34,07,45,67,34>>, <<12,23,56,07,67,67>>,  <<55,23,45,07,89,56>>]

And I need to split it further to:

[ [<<56,45,34>>,<<45,67,34>>], [<<12,23,56>>,<<67,67>>] ,  [<<55,23,45>>, <<89,56>>]  ]

The delimiter in this example is <<07>>

This code process's the binary

parse(Reply) -> binary:split(Reply, <<07>>, [global]).

But now how can I recursively go through the array and do it again.

Here is an example of my current code:

parse(Reply) -> binary:split(Reply, <<01>>, [global]).
parse2(Reply) -> binary:split(Reply, <<07>>, [global]).

func1(Done) -> [parse2(X) || X <- Done].

%%blah blah - get to the executing functions code
Done = parse(Reply),
Done1 = func1(Done),

I know it has to be something super simple the last one sure had me.

Best,
-B

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

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

发布评论

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

评论(2

暗藏城府 2024-09-21 15:50:11

使用列表推导式将函数应用于列表的每个元素。该功能与您的上一个问题几乎相同:

1> L = [<<56,45,34,07,45,67,34>>, <<12,23,56,07,67,67>>,  <<55,23,45,07,89,56>>].
[<<56,45,34,7,45,67,34>>,
 <<12,23,56,7,67,67>>,
 <<55,23,45,7,89,56>>]
2> [binary:split(X, <<07>>, [global]) || X <- L].
[[<<"8-\"">>,<<"-C\"">>],
 [<<12,23,56>>,<<"CC">>],
 [<<55,23,45>>,<<"Y8">>]]
3> io:format("~w~n",[[binary:split(X, <<07>>, [global]) || X <- L]]).
[[<<56,45,34>>,<<45,67,34>>],[<<12,23,56>>,<<67,67>>],[<<55,23,45>>,<<89,56>>]]
ok

另一个选择是使用lists:map/2:

4> lists:map(fun(X) -> binary:split(X, <<07>>, [global]) end, L).
[[<<"8-\"">>,<<"-C\"">>],
 [<<12,23,56>>,<<"CC">>],
 [<<55,23,45>>,<<"Y8">>]]

Use a list comprehensions to apply a function to every element of a list. The function is pretty much the same as for your last question:

1> L = [<<56,45,34,07,45,67,34>>, <<12,23,56,07,67,67>>,  <<55,23,45,07,89,56>>].
[<<56,45,34,7,45,67,34>>,
 <<12,23,56,7,67,67>>,
 <<55,23,45,7,89,56>>]
2> [binary:split(X, <<07>>, [global]) || X <- L].
[[<<"8-\"">>,<<"-C\"">>],
 [<<12,23,56>>,<<"CC">>],
 [<<55,23,45>>,<<"Y8">>]]
3> io:format("~w~n",[[binary:split(X, <<07>>, [global]) || X <- L]]).
[[<<56,45,34>>,<<45,67,34>>],[<<12,23,56>>,<<67,67>>],[<<55,23,45>>,<<89,56>>]]
ok

Another option is to use lists:map/2:

4> lists:map(fun(X) -> binary:split(X, <<07>>, [global]) end, L).
[[<<"8-\"">>,<<"-C\"">>],
 [<<12,23,56>>,<<"CC">>],
 [<<55,23,45>>,<<"Y8">>]]
笑叹一世浮沉 2024-09-21 15:50:11

想通了,请随时告诉我是否有更有效的方法 - 或者 - 只是你的方法,毕竟我正在学习。

parse(Reply) -> binary:split(Reply, <<01>>, [global]).
parse2(<<>>) -> <<>>;
parse2(Reply) -> binary:split(Reply, <<"=">>, [global]).

func2([H|T], Result) ->
    H1 = parse2(H), 
    func2(T, [H1|Result]);
func2([], Result) -> io:format("Reply 1 = ~p~n", [Result]), Result.


Done = parse(Reply),

Done1 = func2(Done, []),

-B

Figured it out please feel free to let me know if there is a more efficient way - or - just your way, I am learning after all.

parse(Reply) -> binary:split(Reply, <<01>>, [global]).
parse2(<<>>) -> <<>>;
parse2(Reply) -> binary:split(Reply, <<"=">>, [global]).

func2([H|T], Result) ->
    H1 = parse2(H), 
    func2(T, [H1|Result]);
func2([], Result) -> io:format("Reply 1 = ~p~n", [Result]), Result.


Done = parse(Reply),

Done1 = func2(Done, []),

-B

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