Opposite of flatten

Ulf Wiger etxuwig@REDACTED
Wed Sep 26 11:24:48 CEST 2001


On Wed, 26 Sep 2001, Nico Weling wrote:

>I need a function which creates tuples and lists from a string,
>something like the opposite of flatten:
>
>1>   Msg = "{ms,{5,{send,filename}}".
>"{ms,{5,{send,filename}}"
>2> Pid ! opposite_of_flatten(Msg).

First of all, Msg above is syntactically incorrect, but I assume
that's just a typo.


1> Str = "{ms,{5,{send,filename}}}".
"{ms,{5,{send,filename}}}"
2> {ok, Toks, _} = erl_scan:string(Str ++ ".").
{ok,[{'{',1},
     {atom,1,ms},
     {',',1},
     {'{',1},
     {integer,1,5},
     {',',1},
     {'{',1},
     {atom,1,send},
     {',',1},
     {atom,1,filename},
     {'}',1},
     {'}',1},
     {'}',1},
     {dot,1}],
    1}
3> erl_parse:parse_term(Toks).
{ok,{ms,{5,{send,filename}}}}


Your function, thus, becomes:

opposite_of_flatten(Str) ->
   {ok, Tokens, _} = erl_scan:string(Str ++ "."),
   {ok, Term} = erl_parse:parse_term(Tokens),
   Term.

Appending the dot is necessary for the parsing to work.

/Uffe
-- 
Ulf Wiger                                    tfn: +46  8 719 81 95
Senior System Architect                      mob: +46 70 519 81 95
Strategic Product & System Management    ATM Multiservice Networks
Data Backbone & Optical Services Division      Ericsson Telecom AB




More information about the erlang-questions mailing list