[erlang-questions] How to pass the length of binary to match into a function that matches the binary?

Russell Brown <>
Mon Nov 24 15:53:45 CET 2014


On 24 Nov 2014, at 13:26, Ulf Wiger <> wrote:

> 
> On 24 Nov 2014, at 13:56, Russell Brown <> wrote:
> 
>> Hi,
>> Is there a way to do this:
>> 
>> my_fun(Bin, Len, <<Bin:Len/binary, Rest/binary>>) ->
>> 	%% do stuff with Bin
>> 	ok.
>> 
>> Len is unbound is the reason I can’t, but it seems like the sort of thing that would be useful if I could.
> 
> Yes, it’s slightly unfortunate, but you can write:
> 
> my_fun(B, Len, Binary) ->
>    case Binary of
>        <<B:Len/binary, Rest/binary>> ->
>            %% do stuff with B
>            ok;
>>    end.

This is what I went with. Thanks Ulf

Russell

> 
> BR,
> Ulf W
> 
> Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
> http://feuerlabs.com
> 
> 
> 



More information about the erlang-questions mailing list