Binary String pattern matching

Eckard Brauer eckard.brauer@REDACTED
Thu Dec 10 11:04:23 CET 2020


Not exactly just matching, but how about first separating integer
characters (and maybe converting them to numbers at the same time)
until a non-integer character appears, then matching:

-module(currtest).
-export([decode_currency/1, decode_currency/2]).

decode_currency(Bin) -> decode_currency(Bin, 0).

decode_currency(<<H,T/binary>>, N)
          when H >= $0, H =< $9 ->
                   decode_currency(T, 10*N+(H-$0));
decode_currency(Bin, N) ->
          case Bin of
                  <<"CHF">> -> {chf, N};
                  <<"EUR">> -> {eur, N};
                  <<"US">> -> {us, N}
          end.

###

<0.81.0> 1> l(currtest), currtest:decode_currency(<<"3123CHF">>).
{chf,3123}
<0.81.0> 2> currtest:decode_currency(<<"341424343EUR">>).
{eur,341424343}


Am Wed, 9 Dec 2020 22:00:03 +0100
schrieb Java House <java4dev@REDACTED>:

> Hi
> 
> yes the binary string is of the format <<"xxx">>
> 
> <<"3123CHF">>
> > <<"341424343EUR">>
> > <<"14143US">>
> >
>  Thank you for the answer.
> So is there any other way?
> erlang provides the binary_to_list, is there any way to achieve the
> same with a list becasue I tried similare patterns with list and it
> does not work e.g
> [V|T] this will always match the first character of the string to V
> and [V|"CHF"] does not work neither.
> 
> Best
> Nikolas
> 
> Στις Τετ, 9 Δεκ 2020 στις 7:58 μ.μ., ο/η kuna.prime <
> kuna.prime@REDACTED> έγραψε:
> 
> > HI,
> >
> > first of all binary string is of form <<"3123CHF">> and not
> > <<3123CHF>> i'm just stating it in case there was a mistake in the
> > first mail
> >
> > to answer your questio in order to pattern match you need to know
> > the size of field you are matching so
> >
> > <<X:4/binary, "CHF">> = <<"3123CHF">>.
> >
> > will match fist 4 bytes and interpret that as new binary (X).
> >
> >
> > Sent with ProtonMail <https://protonmail.com> Secure Email.
> >
> > ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> > On Wednesday, December 9, 2020 7:28 PM, Java House
> > <java4dev@REDACTED> wrote:
> >
> > there is a typo in the example
> > currrency_to_credits({cur, << Value , "CHF">>}, Acc)
> >   when Value >= <<"1000">>, Value =< <<"10000">> ->
> >      {chf, Acc + Value };
> > currrency_to_credits({cur, << Value , "EUR">>}, Acc)
> >   when Value >= <<"1000">>, Value =< <<"10000">> ->
> >      {eur, Acc + Value };
> > currrency_to_credits({cur, << Value , "US">>}, Acc)
> >   when Value >= <<"1000">>, Value =< <<"10000">> ->
> >      {us, Acc + Value };
> >
> > Στις Τετ, 9 Δεκ 2020 στις 7:17 μ.μ., ο/η Java House
> > <java4dev@REDACTED> έγραψε:
> >
> >> Hello all
> >>
> >> I am learning Erlang and have stuck to the following problem.
> >> How to write a function with pattern matching when the parameter
> >> is a binary string.
> >>
> >> I have a list of binary strings e.g.
> >> <<3123CHF>>
> >> <<341424343EUR>>
> >> <<14143US>>
> >>
> >> I am trying to create a function that matches according to a
> >> pattern.
> >>
> >> currrency_to_credits({cur, <<A, "CHF">>}, Acc)
> >>   when Value >= <<"1000">>, Value =< <<"10000">> ->
> >>      {chf, Acc + A};
> >> currrency_to_credits({cur, <<A, "EUR">>}, Acc)
> >>   when Value >= <<"1000">>, Value =< <<"10000">> ->
> >>      {eur, Acc + A};
> >> currrency_to_credits({cur, <<A, "US">>}, Acc)
> >>   when Value >= <<"1000">>, Value =< <<"10000">> ->
> >>      {us, Acc + A};
> >>
> >> But this does not seem to be the right way.
> >> How can I create a pattern for binary string?
> >> will it work better for list string? How?
> >>
> >> Thank you
> >> Nikolas
> >>
> >
> >



-- 
Wir haften nicht für die korrekte Funktion der in dieser eMail
enthaltenen Viren. We are not liable for correct function of the
viruses in this email! :)


More information about the erlang-questions mailing list