将字符串转换为元组在erlang .any指针? [英] converting string to tuple in erlang .any pointer?

查看:183
本文介绍了将字符串转换为元组在erlang .any指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  fe A ={hi,how} 

,我希望将其转换为

  B = { 喜, 如何}。 

当我调用函数list_to_tuple(A)
它给出输出为: - {123, 60,60,34,106,105,100,34,62,62,44,34,104,105,34,125}而不是{hi,how}

解决方案>

您应该使用 erl_scan 模块来标记字符串和 erl_parse 将令牌转换为erlang术语。

 %请注意字符串中表达式末尾的'。'。 
%字符串必须是由'。'终止的有效表达式。
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}


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"}

解决方案

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"}

这篇关于将字符串转换为元组在erlang .any指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆