converting string to tuple in erlang .any pointer?

烈酒焚心 提交于 2019-12-24 14:06:02

问题


how to convert string to tuple in erlang?

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

and i want it to converted into

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

when i call function list_to_tuple(A) it gives output as:-{123,60,60,34,106,105,100,34,62,62,44,34,104,105,34,125} rather than {"hi","how"}


回答1:


You should use erl_scan module to tokenize the string and erl_parse to convert the tokens to a 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"}


来源:https://stackoverflow.com/questions/28578482/converting-string-to-tuple-in-erlang-any-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!