[erlang-questions] Beginners question on pattern matching

Alexander Lamb <>
Fri Oct 5 17:46:55 CEST 2007

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  


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),
				Result = process_lines(Val),

		{error, Why} 	->

				{error, Why}


Then the lines:

do_read(S) ->

	case io:get_line(S,'') of

		eof		-> [];
		Line 		-> [Line | do_read(S)]


Then process each line in the list of lines:

process_lines(Lines) ->

	[Date || [_Code | "," ++ Date] <- Lines].

Something is wrong, because it compiles but here is what I get:

11> c(csv_convert.erl).
12> csv_convert:start().

If each line is actually a list of characters, why can't I match the  
pattern I want???

Thanks for any hint (or examples) !!

Alexander Lamb
Founding Associate

4 ch. de la Tour de Champel
1206 Geneva

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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20071005/1ae6f33e/attachment.html>

More information about the erlang-questions mailing list