Opposite of flatten

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Opposite of flatten

Nico Weling
Hi,

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).

Thanks,

Nico.


Reply | Threaded
Open this post in threaded view
|

Opposite of flatten

Ulf Wiger-4
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



Reply | Threaded
Open this post in threaded view
|

Opposite of flatten

tobbe
In reply to this post by Nico Weling

Does this program do what you want to do ?

Cheers /Tobbe
================= str2tokens.erl ================================
-module(str2tokens).
-compile(export_all).

%% 2> str2tokens:str2terms("{hello,23}. [arne,43]. 5.6. {5.5,1.0}. ").
%% [{hello,23},[arne,43],5.60000,{5.50000,1.00000}]

str2tokenlists("") ->
    [];
str2tokenlists(String) ->
    case erl_scan:tokens([], String, 1) of
        {done, {ok, Tokens, _}, Rest} ->
            [Tokens | str2tokenlists(Rest)]
    end.

tokenlists2terms(Lists) ->
    lists:map(fun(L) ->
                      {ok, Term} = erl_parse:parse_term(L),
                      Term
              end,
              Lists).

str2terms(String) ->
    tokenlists2terms(str2tokenlists(String)).

================================================================


Reply | Threaded
Open this post in threaded view
|

Opposite of flatten

Nico Weling
In reply to this post by Ulf Wiger-4
Hi Ulf,

thanks a lot. Very good and simple solution. It's working fine.

Regards,

Nico.

Ulf Wiger wrote:

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

--
Nico Weling
Software Designer
Ericsson Eurolab Deutschland GmbH
Verification Tool Design

Tel: +49 2407 575 5217
Fax: +49 2407 575 651
Dect:+49 2407 575 89339
mailto:Nico.Weling