Splitting strings in Erlang
string:split and lexemes turn ledger lines into data.
%% string:split and lexemes turn a ledger line into data.
main(_) ->
Entry = "5560199,12,night",
[Number, Mins, Shift] = string:split(Entry, ",", all),
io:format("~s spoke ~s minutes on the ~s shift~n",
[Number, Mins, Shift]),
%% lexemes splits on any of several separators.
Note = "patch 3;test 7 ring 9",
Words = string:lexemes(Note, "; "),
io:format("~p instructions~n", [length(Words)]),
[io:format("- ~s~n", [W]) || W <- Words].
How it works
string:split(Entry, ",", all)breaks every comma.- The result destructures straight into three names.
lexemessplits on any of several separator characters.
Keywords and builtins used here
formatlengthlexemesmainsplit
The run, in numbers
- Lines
- 13
- Characters to type
- 435
- Tokens
- 118
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 118 seconds.
Step 3 of 4 in Text & numbers, step 21 of 31 in Language basics.