Erlang/OTP Forums

Author Message

<  Erlang  ~  Extracting Erlang datatypes from strings

anderst
Posted: Sun Jul 29, 2007 2:41 pm Reply with quote
User Joined: 21 Nov 2006 Posts: 37
Hi,

I using jabber as a protocol to push information to (potentially) 100,000+ users. The clients send back information on their environment as Erlang datatypes. These datatypes are received as a binary. eg:
Code:
<<"[foo,{bar,baz, [1,2,3,4]}]">>


I want to transform this binary to an Erlang datatype with as little code as possible. Performance is not an issue. What I would be inclined to do is

Code:
8> B = binary_to_list( <<"[foo,{bar,baz, [1,2,3,4]}]">>).
"[foo,{bar,baz, [1,2,3,4]}]"
9> erl_scan:string(B).                                   
{ok,[{'[',1},
     {atom,1,foo},
     {',',1},
     {'{',1},
     {atom,1,bar},
     {',',1},
     {atom,1,baz},
     {',',1},
     {'[',1},
     {integer,1,1},
     {',',1},
     {integer,1,2},
     {',',1},
     {integer,1,3},
     {',',1},
     {integer,1,4},
     {']',1},
     {'}',1},
     {']',1}],
    1}


What next? Is there a function I could use to create the Erlang datastructure? I could not find one in the documentation.

Hilsen,
Anders[/code]
View user's profile Send private message Visit poster's website
arcatan
Posted: Sun Jul 29, 2007 6:54 pm Reply with quote
Joined: 19 Jul 2007 Posts: 2 Location: Finland
I don't know if this is the simplest way, but to continue, you could use erl_parse:parse_term. Note that you have to add a dot to the end of the term to make it complete:

Code:

2> B = binary_to_list( <<"[foo,{bar,baz, [1,2,3,4]}].">>).   
"[foo,{bar,baz, [1,2,3,4]}]."
3> {ok, Tokens, _} = erl_scan:string(B).
{ok,[{'[',1},
     {atom,1,foo},
     {',',1},
     {'{',1},
     {atom,1,bar},
     {',',1},
     {atom,1,baz},
     {',',1},
     {'[',1},
     {integer,1,1},
     {',',1},
     {integer,1,2},
     {',',1},
     {integer,1,3},
     {',',1},
     {integer,1,4},
     {']',1},
     {'}',1},
     {']',1},
     {dot,1}],
    1}
4> erl_parse:parse_term(Tokens).
{ok,[foo,{bar,baz,[1,2,3,4]}]}
View user's profile Send private message
alexaandru
Posted: Mon Jul 30, 2007 7:56 am Reply with quote
Benchmarking Hobbist Joined: 19 Jun 2007 Posts: 18 Location: Oradea, Romania
Hi all,

Here's a page from the CookBook -> StringRecipes that may also help you: http://www.trapexit.org/index.php/String_Eval

Good luck,
Alex
View user's profile Send private message
anderst
Posted: Mon Jul 30, 2007 9:05 am Reply with quote
User Joined: 21 Nov 2006 Posts: 37
Under my very nose on trapexit! Thank you all for the quick reply.

Hilsen,
Anders
View user's profile Send private message Visit poster's website
datacompboy
Posted: Thu Aug 30, 2007 5:00 am Reply with quote
User Joined: 21 Sep 2006 Posts: 69 Location: Novosibirsk, Russia
may be better use term_to_binary(X) on sending side, you got binary represention of any term.
on receiver side just do binary_to_term :)

_________________
--- suicide proc near\n call death\n suicide endp
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number
anderst
Posted: Sun Sep 09, 2007 6:16 pm Reply with quote
User Joined: 21 Nov 2006 Posts: 37
Good idea. Unfortunately, the client is lightweight and written in something more appropriate (J**a).

Hilsen,
Anders
View user's profile Send private message Visit poster's website

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum