tags:

views:

60

answers:

2

Hi Everyone, 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

A: 

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>>
cthulahoops
+2  A: 

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? ;-)

Hynek -Pichi- Vychodil