[erlang-questions] tuple looking string/list to tuple

Wes James comptekki@REDACTED
Thu Apr 7 06:58:12 CEST 2011


I'm not sure why Muhammad's (from previous email) is using
gen_tcp:recv(Socket, 0) to receive what looks like a tuple instead of
just sending a message with the tuple, (maybe that is they way it is).

Here is how I changed the Data in to a tuple of atoms or strings/lists:

Z is what Data looks like from his gen_tcp:recv

1> Z= <<"{get,\"user\",\"wilson\",\"lname\"}">>.
<<"{get,\"user\",\"wilson\",\"lname\"}">>
2> Y=binary_to_list(Z).
"{get,\"user\",\"wilson\",\"lname\"}"
3> X=[X || X <- Y, X =/= $\", X=/=${, X=/=$}].
"get,user,wilson,lname"
4> W=string:tokens(X, ",").
["get","user","wilson","lname"]
5> V=list_to_tuple(W).
{"get","user","wilson","lname"}
6>
6> f(X).
ok
7> X=[list_to_atom(X) || X <- W].
[get,user,wilson,lname]
8>
8> f(V).
ok

one liner for binary to tuple of strings

9> V=list_to_tuple(string:tokens([X || X <- Y, X =/= $\", X=/=${,
X=/=$}], ",")).
{"get","user","wilson","lname"}

10>
10> f(V).
ok

one liner for binary to tuple of atoms

11> V=list_to_tuple([list_to_atom(T) || T <- string:tokens([X || X <-
Y, X =/= $\", X=/=${, X=/=$}], ",")]).
{get,user,wilson,lname}


I'd did all this because I wondered what it would take to get a
string/list (or binary in this case) that looked like a tuple in to a
tuple.

-wes



More information about the erlang-questions mailing list