Erlang recipes

17 views
Skip to first unread message

Allan Streib

unread,
May 4, 2011, 2:08:13 PM5/4/11
to erlan...@googlegroups.com
Saw this on hacker news today, thought the group might be interested

http://leanpub.com/erlang-recipes

Cheers,

Allan

ll...@writersglen.com

unread,
May 4, 2011, 4:15:02 PM5/4/11
to erlan...@googlegroups.com
Hello,

I need to extract the first two non-blank lines from a file; e.g. discard all blank ("\n") lines before the first and between the first and second line.

My solutions get rather kludgy. Can anyone suggest an elegant way to do this?

Thanks,

LRP

Garrett Smith

unread,
May 4, 2011, 4:24:06 PM5/4/11
to erlan...@googlegroups.com
If your files aren't larger than available memory:

{ok, Bin} = file:read_file(File),
re:run(Bin, "\n*^(.*)$\n*^(.*)$", [multiline, {capture, all_but_first, list}])

ll...@writersglen.com

unread,
May 4, 2011, 5:49:14 PM5/4/11
to erlan...@googlegroups.com
Thanks, Garrett!

LRP

ll...@writersglen.com

unread,
May 7, 2011, 1:04:47 PM5/7/11
to erlan...@googlegroups.com
I'm trying to understand binary comprehensions.

This works:

<< <<case X of $c -> $C; $i -> $I; X -> X end>> || <<X>> <= <<"The cat in the hat">> >>.
<<"The Cat In the hat">>

This fun, based on the case statement above, compiles:

F = fun (X) -> case X of $c -> $C; $i -> $I; X -> X end end.

But I don't understand how to integrate F into a binary comprehension; e.g.:

These attempts fail:

4> << <<F(X)>> || <<X>> <= <<"The cat in the hat">> >>.
* 1: syntax error before: '('

4> << <<X>> || <<F(X)>> <= <<"The cat in the hat">> >>.
* 1: syntax error before: '('

...as do every other permutation I can think of.

So, is it possible to integrate funs into binary comprehensions and, if so, how?

Thanks,

LRP

ll...@writersglen.com

unread,
May 8, 2011, 11:59:01 PM5/8/11
to erlan...@googlegroups.com
Or maybe I just don't know what I'm doing.

Also posted to TrapExit.

I've spent days trying to use regular expressions to filter binaries with scant success. Here are two experiments that illustrate some of the frustrations I've encountered.

Can some kind soul either show me how to correct my mistakes or, if it so be, confirm that there are quirks in Erlang R14B binaries.

Latex produces typeset text output. Among other things, it outputs both opening and closing quotation marks, consistent with professional typesetting convention. Open quotes in Latex input files, (*.tex), are represented as shown below:

To `quote' in Latex
To ``quote'' in Latex
To ``quote" in Latex

Our goal is to filter and convert a binary derived from a *.txt file to produce proper *.tex markup.

Wierdness 1

Here's a quote represented as a binary:

A = <<"\"The quick brown fox\"">>.
<<"\"The quick brown fox.\"">>

Which displays as we'd expect:

io:format(A,[]).
"The quick brown fox."ok

We can use a regular expression to convert the opening quote to *.tex markup:

B = binary:replace(A,<<"\"">>,<<"``">>).
<<"``The quick brown fox.\"">>

Which also displays as we'd expect:

io:format(B,[]).
``The quick brown fox."ok

But we need to convert opening quotes only which, presumably, can be identified as a double quote character (") followed by a Perl "word" character (\w).

D = binary:replace(A,<<"\"\w">>,<<"``\w">>, [global]).
<<"\"The quick brown fox.\"">>

Display confirms our fears:

io:format(G,[]).
"The quick brown fox."ok

Maybe re will stand us in better stead.

E = re:replace(A,<<"\"">>,<<"``">>, [global,{return, binary}]).
<<"``The quick brown fox.``">>

So far so good. Let's try for opening quotes only:

F = re:replace(A,<<"\"\w">>,<<"``\w">>, [global,{return, binary}]).
<<"\"The quick brown fox.\"">>

io:format(A,[]).
"The quick brown fox."ok

Grrrr.

Wierdness 2

We would like to delete all newlines at the beginning of our binary.

If we know how many, it's easy:

A = <<"\n\n\n\nThe quick brown fox">>.
<<"\n\n\n\nThe quick brown fox">>

B = binary:replace(A,<<"\n\n\n\n">>,<<>>).
<<"The quick brown fox">>

But, following Perl-style regular expressions documented here:

http://www.troubleshooters.com/codecorn/littperl/perlreg.htm

We should be able to delete an arbitrary number of newlines:

C = binary:replace(A,<<"\n{0,}">>,<<>>).
<<"\n\n\n\nThe quick brown fox">>

NOT.

Maybe re can help us out:

5> E = re:replace(A,<<"\n{0, }">>,<<>>, [{return, binary}]).
<<"\n\n\n\nThe quick brown fox">>

NOT. Grrrr.

So what's going on here? Is there a better way?

Reply all
Reply to author
Forward
0 new messages