Thursday, May 13, 2010

Erlang parse file

Trying to parse csv file with the following contents

INTC,07-May-2010 16:51,21.31,21.31,21.31,21.31,200\n
INTC,07-May-2010 16:47,21.31,21.31,21.31,21.31,100\n
INTC,07-May-2010 16:42,21.31,21.31,21.31,21.31,300\n

The erlang code


getCompanyEff(Filename,Company) ->
{ok,Device} = file:open(Filename,read),
getLinesCompany(Device,[],Company).

getLinesCompany(Device,Accum,Company) ->
case io:get_line(Device,"") of
eof -> Accum;
Line ->
case regexp:first_match(Line, Company ++ ",*") of
{match,_,_} -> getLinesCompany(Device, [Line|Accum], Company);
nomatch -> getLinesCompany(Device, Accum, Company)
end
end.

and to execute:
erl> module:getCompanyEff("/path/to/file", "CompanyName").

No comments:

Post a Comment