ERlang - 尝试查找列表成分的长度

发布于 2024-09-14 20:15:24 字数 830 浏览 2 评论 0原文

猜猜已经晚了,我是一个初学者,只需要一点帮助..

我试图找到列表的长度..但不是列表本身,而是其中值的长度..

我采取类似的东西:

   Other = [<<"366">>,0,
             <<1>>,
             <<"344">>,<<"Really"
             <<1>>,
             <<"989">>,<<"NotReally">>
             <<1>>,
             <<"345">>,4,
             <<1>>,
             <<"155">>,"209.191"]

我确实想首先将 Other 转换为其 RAW 组成二进制文件

示例:

Other = [<<3>>,<<4>>,<<7>>,<<56>>,<<45>>,<<56>>...]

当然,这是原始 Other 的示例(不是正确的转换值)。这样里面的所有值都是最基本的二进制数据。

然后我可以简单地迭代计算每个 <<_>>并确定总消息长度。

希望我足够清楚能够找到解决方案。

感谢大家的帮助,GN

guess its getting late, and Im a beginner, just need a little help..

Im trying to find the length of a list.. BUT NOT of the lists themselves, rather the length of the values within..

I take something like:

   Other = [<<"366">>,0,
             <<1>>,
             <<"344">>,<<"Really"
             <<1>>,
             <<"989">>,<<"NotReally">>
             <<1>>,
             <<"345">>,4,
             <<1>>,
             <<"155">>,"209.191"]

I would really want to first convert Other into its RAW constituent binary

Example:

Other = [<<3>>,<<4>>,<<7>>,<<56>>,<<45>>,<<56>>...]

This, of course, is an example of way the original Other would look like(Not right conversion values). So that all of the values in there are there most basic binary data.

Then I could simply iterate through counting each <<_>> and determining the total message length.

Hope I was clear enough to find a solution.

Thanks all for the help, GN

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

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

发布评论

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

评论(2

内心激荡 2024-09-21 20:15:24

iolist_size/1 就是你寻找。

1> iolist_size([<<"366">>,0,<<1>>,<<"344">>,<<"Really">>,<<1>>,<<"989">>,<<"NotReally">>,<<1>>,<<"345">>,4,<<1>>,<<"155">>,"209.191"]).                 
43
2> v(1) - 1.
42

PS:为什么你的示例数据有这一多余特征? ;-)

iolist_size/1 is what you are looking for.

1> iolist_size([<<"366">>,0,<<1>>,<<"344">>,<<"Really">>,<<1>>,<<"989">>,<<"NotReally">>,<<1>>,<<"345">>,4,<<1>>,<<"155">>,"209.191"]).                 
43
2> v(1) - 1.
42

P.S.: Why your example data have this one surplus character? ;-)

我们的影子 2024-09-21 20:15:24

如果你想做的只是找到整个结构的长度,我会尝试这样的事情:

my_length(X) when is_integer(X) -> 1;
my_length(X) when is_binary(X) -> erlang:size(X);
my_length(Lst) when is_list(Lst) ->
    lists:sum([my_length(X) || X <- Lst]).

如果你真的想构建结构的平面版本,那么 erlang:list_to_binary 可以让你非常接近你需要的东西,然后就调用 size 即可。 (实际上,这可能比我的第一次尝试更好。)

1> erlang:list_to_binary([<<"366">>,0,<<"155">>,"209.191"]).
<<51,54,54,0,49,53,53,50,48,57,46,49,57,49>>

If all you're trying to do is find the length of the entire structure, I'd try something like this:

my_length(X) when is_integer(X) -> 1;
my_length(X) when is_binary(X) -> erlang:size(X);
my_length(Lst) when is_list(Lst) ->
    lists:sum([my_length(X) || X <- Lst]).

If you really want to build a flat version of your structure, then erlang:list_to_binary gets you pretty close to what you need, then just call size on that. (Actually, this may be better than my first attempt.)

1> erlang:list_to_binary([<<"366">>,0,<<"155">>,"209.191"]).
<<51,54,54,0,49,53,53,50,48,57,46,49,57,49>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文