nabil chouaib
nabil chouaib

Reputation: 135

extract data from string in erlang

I declared Line variable with this syntax :

Line ="1;nabil;chouaib;france;27",

I want to declared 5 variables : Id , Firsname , lastname , address , age

The Id should contains 1 The Firsname should contains nabil The lastname should contains chouaib The address should contains france The age should contains 27

so I have to parse the contents of the variable Line

The separator in thi case is ;

I have this function :

test()->

    ForEachLine = fun(Line,Buffer)-> 
                          io:format("Line: ~s~n",Line),


                          [IdStr, FirstnameStr, LastnameStr, AddressTr, AgeStr] = string:tokens(Line, ";"),
   Buffer end,

 InitialBuffer = [],

 csv:parse("/var/lib/mysql/test/person1.csv",ForEachLine,InitialBuffer).

but when I excecute the test function T have this error :

1> model:test().
Line: 1;nabil;chouaib;france;27
** exception error: no match of right hand side value [["1;nabil;chouaib;france;27"]]
     in function  model:'-test/0-fun-0-'/2
     in call from csv:start_parsing/3

the problem is related to this line :

[IdStr, Firstname, Lastname, Address, AgeStr] = string:tokens(Line, ";") 

I think that the type of Line is not a string ( but in the consol before the error this line is displayed Line: 1;nabil;chouaib;france;27 )

so I should just know the type of Line

the csv.erl module is :

%%% --- csv parser in Erlang. ------
%%% To help process large csv files without loading them into
%%% memory. Similar to the xml parsing technique of SAX

-module(csv).
-compile(export_all).

parse(FilePath,ForEachLine,Opaque)->
    case file:open(FilePath,[read]) of
        {_,S} ->
            start_parsing(S,ForEachLine,Opaque);
        Error -> Error
    end.

start_parsing(S,ForEachLine,Opaque)->
    Line = io:get_line(S,''),
    case Line of
        eof -> {ok,Opaque};
        "\n" -> start_parsing(S,ForEachLine,Opaque);
        "\r\n" -> start_parsing(S,ForEachLine,Opaque);
        _ -> 
            NewOpaque = ForEachLine(scanner(clean(clean(Line,10),13)),Opaque),
            start_parsing(S,ForEachLine,NewOpaque)
    end.

scan(InitString,Char,[Head|Buffer]) when Head == Char -> 
    {lists:reverse(InitString),Buffer};
scan(InitString,Char,[Head|Buffer]) when Head =/= Char ->
    scan([Head|InitString],Char,Buffer);
scan(X,_,Buffer) when Buffer == [] -> {done,lists:reverse(X)}.
scanner(Text)-> lists:reverse(traverse_text(Text,[])).

traverse_text(Text,Buff)->
    case scan("",$,,Text) of
        {done,SomeText}-> [SomeText|Buff];
        {Value,Rem}-> traverse_text(Rem,[Value|Buff])
    end.

clean(Text,Char)-> 
    string:strip(string:strip(Text,right,Char),left,Char).

Upvotes: 0

Views: 3032

Answers (1)

hdima
hdima

Reputation: 3637

You can split the string into a list of strings by using string:tokens/2 function and pattern match the list to extract the variables:

1> [IdStr, Firstname, Lastname, Address, AgeStr] = string:tokens(Line, ";").
["1","nabil","chouaib","france","27"]

And then IdStr and AgeStr can be converted to integers by using list_to_integer/1 function:

2> Id = list_to_integer(IdStr).
1
3> Age = list_to_integer(AgeStr).
27

Upvotes: 7

Related Questions