<div dir="ltr">Edwin , thanks for your response! :)<br><br><div class="gmail_quote">2008/7/19 Edwin Fine <<a href="mailto:emofine@gmail.com">emofine@gmail.com</a>>:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Litao,<br><br>I think this is a bug in Erlang R12B-3. Certainly the documentation can be misleading, because in Programming Examples (Section 4.6: Matching Binaries), it specifically says that this construct is not allowed:<br>
<span><br><b>"Size</b></span><b> must be an integer literal, or a previously bound
variable. Note that the following is not allowed:
</b>
<div><pre><font size="4"><b>foo(N, <<X:N,T/binary>>) -><br> {X,T}.<br></b></font></pre></div>
<p><b>The two occurrences of <span>N</span> are not related. The compiler
will complain that the <span>N</span> in the size field is unbound."
</b></p>
<br>That being said, if you rewrite the expression as shown below, it will compile (but does not work). If I understand correctly, binary, bits, and bitstream are the same, except that the default bit size for binary is 8, and for bitstring it is 1. Since you are overriding the default size anyway, you can use binary-unit:11 in place of bits-unit:11.<br>
<br>decode(<<N:5,Chans:N/binary-unit:11,_/bits>>) -><br><div dir="ltr"> [Chan || <<Chan:11>> <- Chans].<br></div><br><br>103> Chans3 = <<3:5,2:11,3:11,4:11>>. <br><<24,2,0,96,4:6>><br>
104> bb:decode(Chans3).<br>N:3, Chans:<<0,64,12,2,0:1>><br>** exception error: no case clause matching {<<0,64,12,2,0:1>>}<br> in function bb:'-decode/1-lc$^0/1-0-'/1<br>105> <br>
<br>The code is:<br><br>-module(bb).<br>-compile([export_all]).<br><br>decode(<<N:5,Chans:N/binary-unit:11,_/bits>>) -><br> io:format("N:~p, Chans:~p~n", [N, Chans]),<br> [Chan || <<Chan:11>> <- Chans].<br>
<br>So even though the programming examples say that you can't use the same N in the match, it actually does work, but the list comprehension does not. I found out this is because a bitstring generator has to use "<=" and not "<-". So the final working code is:<br>
<br>-module(bb).<br>-compile([export_all]).<br><br>decode(<<N:5,Chans:N/binary-unit:11,_/bits>>) -><br> [Chan || <<Chan:11>> <b style="color: rgb(255, 0, 0);"><=</b> Chans].<br><br>118> c(bb).<br>
{ok,bb}<br>119> Chans3 = <<3:5,2:11,3:11,4:11>>.<br><<24,2,0,96,4:6>><br>120> bb:decode(Chans3). <br>[2,3,4]<br><br>Hope this helps.<br><br><br><br><div class="gmail_quote">2008/7/18 litao cheng <<a href="mailto:litaocheng@gmail.com" target="_blank">litaocheng@gmail.com</a>>:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div class="Wj3C7c"><div dir="ltr">hi, all.<br>when I read this paper: Programming Efficiently with Binaries and Bit Strings <a href="http://www.erlang.se/euc/07/papers/1700Gustafsson.pdf" target="_blank">http://www.erlang.se/euc/07/papers/1700Gustafsson.pdf</a>, I encounter a compile error, the code snipes is a example to parse the IS 683-PRL protocol:<br>
<br>decode(<<N:5,Chans:N/bits-unit:11,_/bits>>) -><br> [Chan || <<Chan:11>> <- Chans].<br><br>the compiler says:<br> bit type mismatch (unit) between 11 and 1<br><br>I read the erlang reference mannual, the bits unit default is 1, I think the unit can be set, why this compile error occur? thank you!<br>
my erlang emulator is 5.6.3(R12B-3).<br></div>
<br></div></div>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br></blockquote></div><font color="#888888"><br><br clear="all"><br>-- <br>The great enemy of the truth is very often not the lie -- deliberate, contrived and dishonest, but the myth, persistent, persuasive, and unrealistic. Belief in myths allows the comfort of opinion without the discomfort of thought.<br>
John F. Kennedy 35th president of US 1961-1963 (1917 - 1963)
</font></blockquote></div><br></div>