| Author |
Message |
< Erlang ~ Extracting Erlang datatypes from strings |
| anderst |
Posted: Sun Jul 29, 2007 2:41 pm |
|
|
|
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] |
|
|
| Back to top |
|
| arcatan |
Posted: Sun Jul 29, 2007 6:54 pm |
|
|
|
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]}]}
|
|
|
| Back to top |
|
| alexaandru |
Posted: Mon Jul 30, 2007 7:56 am |
|
|
|
Benchmarking Hobbist
Joined: 19 Jun 2007
Posts: 18
Location: Oradea, Romania
|
|
| Back to top |
|
| anderst |
Posted: Mon Jul 30, 2007 9:05 am |
|
|
|
User
Joined: 21 Nov 2006
Posts: 37
|
Under my very nose on trapexit! Thank you all for the quick reply.
Hilsen,
Anders |
|
|
| Back to top |
|
| datacompboy |
Posted: Thu Aug 30, 2007 5:00 am |
|
|
|
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 |
|
| Back to top |
|
| anderst |
Posted: Sun Sep 09, 2007 6:16 pm |
|
|
|
User
Joined: 21 Nov 2006
Posts: 37
|
Good idea. Unfortunately, the client is lightweight and written in something more appropriate (J**a).
Hilsen,
Anders |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
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
|
|
|