Mon Mar 3 10:51:05 CET 2008
On 03/03/2008, Balathasan Sayanthan <bsayanthan@REDACTED> wrote:
> Hi ,
> I am trying to write a decoding logic to decode a list which has a
> predefined separator ( eg. tab). But the distance at which the separator
> will occur from the start of the list is unknown. I want to extract all the
> values before the separator into one list and the rest of the list into
> another, what would be the best way of doing this in Erlang?
> for example if the program is given a list like [ 23, 34, 9, 34, 78, 90, 9,
> 10, 34 ]
> I want this to be split into [23, 34] , [ 34, 78, 90, 9, 10, 34] using 9 as
> the separator(the point at which will occur is a variable and aslo there
> might be multiple occurances of it).
> Am I supposed to use the string:tokens(string, substring), but is there a
> more efficient way of doing it in Erlang?
You can quite easily write a function to do this.
split_at(List, Split_char) ->
split_at(List, Split_char, ).
split_at([Split_char | T], Split_char, Acc) ->
split_at([H | T], Split_char, Acc) ->
split_at(T, Split_char, [H | Acc]);
split_at(, _, Acc) ->
Alternatively, have a look at lists:splitwith/2
More information about the erlang-questions