Convert string to tuple in erlang.any pointer?

how to convert string to tuple in erlang?

f.e A="{"hi","how"}"

and I want it to convert to

B={"hi","how"}.

when I call the list_to_tuple (A) function it gives the result as: - {123,60,60,34,106,105,100,34,62,62,44,34,104,105,34,125}, and not {"hi", "how"}

+1
source share
1 answer

You should use the erl_scan module to tokenize the string and erl_parse to convert tokens to the erlang term.

% Note the '.' at the end of the expression inside string.
% The string has to be a valid expression terminated by a '.'.
1> Str = "{\"x\",\"y\"}.".  
"{\"x\",\"y\"}."
2> {ok, Ts, _} = erl_scan:string(Str).
{ok,[{'{',1},
     {string,1,"x"},
     {',',1},
     {string,1,"y"},
     {'}',1},
     {dot,1}],
    1}
3> {ok, Tup} = erl_parse:parse_term(Ts).
{ok,{"x","y"}}
4> Tup.
{"x","y"}
+2
source

Source: https://habr.com/ru/post/1608576/


All Articles