nested case error

Per Gustafsson per.gustafsson@REDACTED
Mon Jun 6 11:29:32 CEST 2005


Maybe you should have a look at the bit syntax. It is described in 
chapter 6.16 of the erlang reference manual. It would allow you to 
rewrite your function in the following manner:

setupSocket(State) ->
    case gen_tcp:recv(State#state.sock, 0, ?server_idle_timeout) of
      {ok, Bin} ->
            <<Length:16,Major,Minor,Release,CType,SessionId:32,
                UIdLength,UId:UIdLength/binary,
                PwLength,Pw:PwLength/binary,_/binary>> = Bin,
            Version = [Major,Minor,Release],
            UserId = binary_to_list(UId),
            Password = binary_to_list(Pw),
            ClientType =
                case CType of
                    0 -> type1;
                    1 -> type2;
                    _ -> unknown
                end,
            
io:format("Length\tVersion\tClientType\tSessionId\tUserIdLength\tUserId\tPasswordLength\tPassword",[]),
            io:format("~p\t~p\t~p\t~p\t~p\t~p\t~p\t~p", [Length, 
Version, ClientType, SessionId,
                             UIdLength, UserId, PwLength, Password]);
        Else ->
            io:format("~p", [Else])
     end.

Per Gustafsson

> Hello list,
> I have tried many punctuation styles around this nested case code 
> example, but can't get it to compile.
> See line 15, where I have an end to the nested case and the last line 
> where I end the outer case.  I apologize for email formating, its the 
> best Thunderbird would do...
> The compiler error on this code sample says I have an error before ';' 
> on line 73 which is the end of the first clause of the outer case
>
> setupSocket(State) ->
>     case gen_tcp:recv(State#state.sock, 0, ?server_idle_timeout) of
>       {ok, Bin} ->
>         [Two, One] = binary_to_list(Bin, 1, 2),
>         Length = (Two * 256) + One,
>         Version = [Major, Minor, Release] = binary_to_list(Bin, 3, 5),
>        
>         case binary_to_list(Bin, 6, 6) of
>             [0] ->
>                 ClientType = type1;
>             [1] ->
>                 ClientType = type2;
>             [_Else] ->
>                 ClientType = unknown
>         end, %% end of inner case
>         [Four, Three, Two, One] = binary_to_list(Bin, 7, 10),
>         SessionId = (Four * 16777216) + (Three * 65536)+ (Two * 256) + 
> One,
>         UserIdLength = binary_to_list(Bin, 11, 11),
>         UserId = binary_to_list(Bin, 12, 12 + UserIdLength),
>         PasswordLength = binary_to_list(Bin, 13 + UserIdLength, 13 + 
> UserIdLength),
>         Password = binary_to_list(Bin, 14 + UserIdLength, 14 + 
> PasswordLength),
> io:format("Length\tVersion\tClientType\tSessionId\tUserIdLength\tUserId\tPasswordLength\tPassword",[]), 
>
>         io:format("~p\t~p\t~p\t~p\t~p\t~p\t~p\t~p", [Length, Version, 
> ClientType, SessionId, UserIdLength, UserId, PasswordLength, 
> Password];  %% ERROR here - end of outer case first clause
>       Else ->
>         io:format(~p, [Else]) %% end of outer case second clause
>     end. %% end of outer case
>
>
> thanks ke han





More information about the erlang-questions mailing list