Reputation: 9927
I am a newbie in erlang. I have to maintain and implement new feature of an an backend project built in erlang. I have strong background in c/c++, php, ruby, python and javascript. I have had problem in implementing a feature to remove some of the prefixes from list of phone number
-module(reminder_group).
-export([address_with_no_prefix/1]).
-define(PREFIXES, [
'855',
'+855',
'0',
'+0',
'+'
]).
address_with_no_prefix(Address) ->
address_with_no_prefix(Address, ?PREFIXES).
address_with_no_prefix(Address, []) -> Address;
address_with_no_prefix(Address, Prefixes) ->
[Prefix|H] = Prefixes,
Length = string:len(Prefix),
AddressPrefix = string:substr(Address, 1, Length),
if
Prefix == AddressPrefix ->
string:substr(Address, Length);
true ->
address_with_no_prefix(Address, H)
end.
After compiling I run
reminder_group:address_with_no_prefix("0123445")
I got the following error:
1> reminder_group:address_with_no_prefix("0123445").
** exception error: bad argument
in function length/1
called as length('855')
in call from string:len/1 (string.erl, line 66)
in call from reminder_group:address_with_no_prefix/2 (src/models/reminder_group.erl, line 34)
It seems like problem of Length = string:len(Prefix) however I test to run
string:len("855").
it works fine with the result of 3. Is there anything wrong with my list of string value ? Please help.
Upvotes: 3
Views: 173
Reputation: 13945
You're using single quotes (rather than double quotes) to denote your prefixes. I assume you meant for these to be strings, not atoms (hence applying string:len
)?
Your test worked because you correctly used double quotes to construct a string literal.
Upvotes: 3