[erlang-questions] Reading a term from a string

Caoyuan <>
Fri Aug 24 17:33:06 CEST 2007


Did you mean:

> TupleListStr = "[{a, b, c}, {d, e}]".
"[{a, b, c}, {d, e}]"
> {ok, TermTokens, _EndLine} = erl_scan:string(TupleListStr ++ ".").
{ok,[{'[',1},
    {'{',1},
    {atom,1,a},
    {',',1},
    {atom,1,b},
    {',',1},
    {atom,1,c},
    {'}',1},
    {',',1},
    {'{',1},
    {atom,1,d},
    {',',1},
    {atom,1,e},
    {'}',1},
    {']',1},
    {dot,1}],
   1}
> {ok, Exprs} = erl_parse:parse_exprs(TermTokens).
{ok,[{cons,1,
          {tuple,1,[{atom,1,a},{atom,1,b},{atom,1,c}]},
          {cons,1,{tuple,1,[{atom,1,d},{atom,1,e}]},{nil,1}}}]}
> {value, TupleList, _NewBindings} = erl_eval:exprs(Exprs, []).
{value,[{a,b,c},{d,e}],[]}
> TupleList.
[{a,b,c},{d,e}]

On 8/24/07, Joel Reymont <> wrote:
> How do you read, say, a list of tuples from a string?
>
>         Thanks, Joel
>
> --
> http://wagerlabs.com
>
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>


-- 
- Caoyuan



More information about the erlang-questions mailing list