Binary String pattern matching

Java House java4dev@REDACTED
Wed Dec 9 21:52:25 CET 2020


Hi

data format cannot change, its coming from the provider
I only the list of different currencies.
The value can be anything from 1 digit to 4
thank you

Στις Τετ, 9 Δεκ 2020 στις 7:58 μ.μ., ο/η Harenson Henao <harenson@REDACTED>
έγραψε:

> You may want to move the currency to the front of the bitstring and then
> pattern match it like: currency_to_credits({cur, <<"EUR", Amount/binary>>},
> Acc) ...
>
> On Wed, Dec 9, 2020 at 1:49 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
>>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20201209/b42835f3/attachment.htm>


More information about the erlang-questions mailing list