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

one liner for binary to tuple of strings

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

10> f(V).

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=/=$}], ",")]).

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


More information about the erlang-questions mailing list