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

Ulf Wiger ulf@REDACTED
Mon Nov 24 14:26:26 CET 2014

On 24 Nov 2014, at 13:56, Russell Brown <russelldb@REDACTED> 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

Ulf W

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 496 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141124/c9c910e9/attachment.bin>

More information about the erlang-questions mailing list