| Author |
Message |
< Erlang ~ Erlang bit syntax: matching a NULL byte |
| parijat |
Posted: Sun Aug 19, 2007 12:41 pm |
|
|
|
Joined: 19 Aug 2007
Posts: 4
Location: Singapore
|
Newbie learning Erlang. This is not exactly a problem; more like an observation.
The Erlang bit syntax is wonderful, if complicated.
Say you have a Binary like:
Code:
84> Bin1 = <<1,0,2>>.
<<1,0,2>>
Then, this will match:
Code: 85> <<A:1/binary, B:8, C:1/binary>> = Bin1.
<<1,0,2>> %% A => 1, C => 2
But this will not match:
Code: 86> <<A:1/binary, B:1/binary, C:1/binary>> = Bin1.
=ERROR REPORT==== 19-Aug-2007::17:46:17 ===
Error in process <0.139.0> with exit value: {{badmatch,<<3>>},[{erl_eval,expr,3}]}
** exited: {{badmatch,<<1,0,2>>,[{erl_eval,expr,3}]} **
It seems that I can specify a literal '0' as an int type (the default type) but not a binary type (see http://erlang.org/doc/reference_manual/expressions.html#bit_syntax for the gory details). Wonder why? One day I will read enough of the reference manual to understand. |
|
|
| Back to top |
|
| khigia |
Posted: Mon Aug 20, 2007 1:47 am |
|
|
|
User
Joined: 27 Mar 2007
Posts: 52
|
Looking at the code, seems you entered all the expressions in a single erlang shell ... and the error on input 86 is probably not a binary matching problem.
In fact after the fisrt match line 85, variables A,B,C have values and types, with B being an integer of value 0. When matching again line 86, A and C match the binary but B do not match because the pattern line 86 is binary where B is already defined as an integer.
Note: in the shell, the function f/1 enable to "forget" the definition of a variable. |
|
|
| Back to top |
|
| parijat |
Posted: Mon Aug 20, 2007 7:21 am |
|
|
|
Joined: 19 Aug 2007
Posts: 4
Location: Singapore
|
khigia wrote:
In fact after the fisrt match line 85, variables A,B,C have values and types, with B being an integer of value 0. When matching again line 86, A and C match the binary but B do not match because the pattern line 86 is binary where B is already defined as an integer.
Oh, yes. That is correct. Indeed, doing this works:
Code:
1> Bin = <<1, 0, 2>>.
<<1,0,2>>
2> <<A1:1/binary, B1:1/binary, C1:1/binary>>.
** 1: variable 'A1' is unbound **
3> <<A1:1/binary, B1:1/binary, C1:1/binary>> = Bin.
<<1,0,2>>
4> {A1, B1, C1}.
{<<1>>,<<0>>,<<2>>}
5> <<A2:1/binary, B2:8, C2:1/binary>> = Bin.
<<1,0,2>>
6> {A2, B2, C2}.
{<<1>>,0,<<2>>}
|
|
|
| Back to top |
|
| parijat |
Posted: Mon Aug 20, 2007 3:10 pm |
|
|
|
Joined: 19 Aug 2007
Posts: 4
Location: Singapore
|
Hmm.. the problem that I originally faced was this:
Code:
(emacs@SIEGFRIED)1> Bin = <<1,0,2>>.
<<1,0,2>>
(emacs@SIEGFRIED)2> <<1:1/binary, 0:1/binary, 2:1/binary>> = Bin.
=ERROR REPORT==== 20-Aug-2007::20:35:47 ===
Error in process <0.35.0> on node 'emacs@SIEGFRIED' with exit value: {{badmatch,<<3 bytes>>},[{erl_eval,expr,3}]}
** exited: {{badmatch,<<1,0,2>>},[{erl_eval,expr,3}]} **
(emacs@SIEGFRIED)3> <<1:8, 0:8, 2:8>> = Bin.
<<1,0,2>>
That is, 0:1/binary does not match a null byte, but 0:8 does.
Sorry for getting lost while trying to "simplify" the problem. |
|
|
| Back to top |
|
| khigia |
Posted: Tue Aug 21, 2007 12:28 am |
|
|
|
User
Joined: 27 Mar 2007
Posts: 52
|
I think it is the expected behaviour.
Line 3, it is possible to match the first byte of Bin to an integer of value 1, the second byte to a integer of value 0 and the third to integer 2.
But line 2, it is not possible to match the first byte of Bin to a 8bits binary of value "integer 1", the second byte of Bin to a 8bits binary of value "integer 0" etc
Matching <<A:1/binary, B:1/binary, C:1/binary>> = Bin
result in A = <<1>>, B = <<0>> and C = <<2>>,
not in A=1, B=0, C=2. |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|