[erlang-questions] problem with string:tokens

Richard O'Keefe ok@REDACTED
Wed Jul 1 04:05:44 CEST 2009


On Jul 1, 2009, at 1:16 AM, info wrote:

> Hi All,
>
> I have two strings: S1="A,,C,D" and S2="A,B,,D"
>
> I do string:tokens(S1) :  "A","C","D"  length =3
> I do string:tokens(S2) :  "A","B","D"  length = 3
>
> Imagine the string S = S1 or S2
> How can I know if B is absent or C ?
>
> It is a general question of course ! the tokens function cannot  
> separate the tokens with a composite string unfortunately (here ",,")

There is probably something else in the library you could use,
but I rolled my own.  So could you.

2> s:separate("A,,C,D", $,).
["A",[],"C","D"]
3> s:separate("A,B,,D", ",").
["A","B",[],"D"]

separate(Characters, Separator)
  - Characters must be a flat list of character codes
  - Separator may be a single character $x or
    a non-empty flat list of character codes "x" "xyz" ...
  - breaks Characters into X1++Separator++X2++Separator++...++Xn
    and returns [X1,...,Xn].

separate(Chars, [S|Sep])
   when is_list(Chars), is_integer(S), S >= 0, is_list(Sep) ->
     separate(Chars, S, Sep);
separate(Chars, S)
   when is_list(Chars), is_integer(S), S >= 0 ->
     separate(Chars, S, []).

separate(Chars, S, Sep) ->
     case part(Chars, S, Sep, [])
       of no ->          [Chars]
        ; {Word,Tail} -> [Word|separate(Tail, S, Sep)]
     end.

part([], _, _, _) ->
     no;
part([C|Chars], C, Sep, Rev) ->
     case prefix_check(Chars, Sep)
       of no   -> part(Chars, C, Sep, [C|Rev])
        ; Tail -> {lists:reverse(Rev), Tail}
     end;
part([X|Chars], C, Sep, Rev) ->
     part(Chars, C, Sep, [X|Rev]).

prefix_check(Tail, []) ->
     Tail;
prefix_check([X|Tail], [X|Sep]) ->
     prefix_check(Tail, Sep);
prefix_check(_, _) ->
     no.





More information about the erlang-questions mailing list