Parsing formatted text

Rudolph van Graan rvg@REDACTED
Wed Jul 13 08:26:06 CEST 2005


Hey,

string:tokens will work, however, it will not return the second last  
field (empty one between two commas) and silently discard it:

(rvg@REDACTED)2> string:tokens("TRADE,X_._., 
20040901,00:00:02,1105.25,,1",",").
["TRADE","X_._.","20040901","00:00:02","1105.25","1"]

Below a version that is strict (Note the empty list in 2nd last  
position)

(rvg@REDACTED)3> customlists:tokens("TRADE,X_._., 
20040901,00:00:02,1105.25,,1",",").
["TRADE","X_._.","20040901","00:00:02","1105.25",[],"1"]

%%% 
======================================================================== 
==================

tokens(S, Tokens) ->
   tokens(S, Tokens, [], []).


%%% tokens(Input,Tokens,Current,Result)

tokens([],Tokens,[],Result) ->
   lists:reverse(Result);

tokens([],Tokens,Current,Result) ->
   tokens([],Tokens,[],[lists:reverse(Current)|Result]);

tokens([C|S],Tokens,Current,Result) ->
   case lists:member(C, Tokens) of
     true ->
       case S of
         [] ->
           tokens(S,Tokens,[],[""|[lists:reverse(Current)|Result]]);
         _ ->
           tokens(S,Tokens,[],[lists:reverse(Current)|Result])
       end;
     false ->
       tokens(S,Tokens,[C|Current],Result)
   end.


Rudolph


On 12 Jul 2005, at 11:27 AM, Joel Reymont wrote:

> Folks,
>
> How would you parse a line like this in Erlang?
>
> TRADE,X_._.,20040901,00:00:02,1105.25,,1
>
> I would need to skip the first two fields and convert the rest from  
> ascii.
>
>     Thanks, Joel
>
> --
> http://wagerlabs.com/uptick
>
>
>
>
>




More information about the erlang-questions mailing list