Binary String pattern matching
Håkan Huss
huss01@REDACTED
Thu Dec 10 10:01:40 CET 2020
For lists there is no way. For binaries it is actually possible using
guards:
currency_to_credits({cur, V}, Acc) when binary_part(V, {byte_size(V), -3})
== <<"CHF">>,
binary_part(V, {0, byte_size(V) -
3}) >= <<"1000">>,
binary_part(V, {0, byte_size(V) -
3}) =< <<"10000">> ->
{chf, Acc + binary_to_integer(binary:part(V, {0, byte_size(V) - 3}))};
...
However, I would argue that it's much more readable if you split the binary
in currency_to_credits and pass on the parts to a helper function that
handles the logic.
Regards,
/Håkan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20201210/72ed8962/attachment.htm>
More information about the erlang-questions
mailing list