[英]ERlang - Trying to find LENGTH of constituents of List
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我真的很想先将 Other 转换为它的 RAW 组成二进制文件
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感谢大家的帮助,GN
iolist_size/1
is what you are looking for. 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: Why your example data have this one surplus character? PS:为什么你的示例数据有这个多余的字符? ;-)
;-)
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.如果你真的想构建一个平面版本的结构,那么 erlang:list_to_binary 会让你非常接近你需要的东西,然后只需调用 size 即可。 (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>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.