[erlang-questions] Beginners question on pattern matching, thank you

Alexander Lamb alexander.lamb@REDACTED
Sat Oct 6 22:20:10 CEST 2007


Yes, my mistake was obviously to mistake pattern matching with some  
king of regular expression handling.

Thanks a lot for your help.

Alex

Le 5 oct. 07 à 18:32, Alexander Petry a écrit :

> * Alexander Lamb <alexander.lamb@REDACTED> [071005 18:06]:
>> Hello List,
>>
>> I am starting on Erlang. I read most of the new Erlang book. However,
>> I am lost with something really simple and probably stupid.
>>
>> I am reading a file, line by line.
>>
>> Each line is in the form
>>
>> "a_code" , a_date
>>
>> The quotes (") are present in the file, as well as the comma. For
>> example:
>>
>> "C51CBA979E4311D67900201842D2EEE81",2003-08-07
>>
>> I would like to cut the string into pieces (to be able to modify
>> it...). Again, this is a test to learn Erlang, I know how to do this
>> in two lines of Ruby :-). I could also probably use a built in
>> function to parse the string. But I would like to understand pattern
>> matching and lists.
>>
>> So I wrote this, first read the file:
>>
>> start() ->
>>
>> 	case file:open("ENRL_ENROLLMENT.csv",read) of
>>
>> 		{ok, S} 	->
>> 				Val = do_read(S),
>> 				file:close(S),
>> 				Result = process_lines(Val),
>> 				{ok,Result};
>>
>> 		{error, Why} 	->
>>
>> 				{error, Why}
>>
>> 	end.
>>
>> Then the lines:
>>
>> do_read(S) ->
>>
>> 	case io:get_line(S,'') of
>>
>> 		eof		-> [];
>> 		Line 		-> [Line | do_read(S)]
>>
>> 	end.
>>
>> Then process each line in the list of lines:
>>
>> process_lines(Lines) ->
>>
>> 	[Date || [_Code | "," ++ Date] <- Lines].
>
> I am new to Erlang, too, but i guess that _Code does only match a  
> single
> element of the list, i.e in your case the first ". A simple  
> solution would be
> to split up a single line like:
>
> 	[_Code, Date] = string:split(Line, ","),
>
> Regards,
> Alex, too ;-)
>

--
Alexander Lamb
Founding Associate
RODANOTECH Sàrl

4 ch. de la Tour de Champel
1206 Geneva
Switzerland

Tel:  022 347 77 37
Fax: 022 347 77 38

http://www.rodanotech.ch



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20071006/a56901a3/attachment.htm>


More information about the erlang-questions mailing list