[erlang-questions] Records: How can I dump the structure of a record?

Edwin Fine <>
Mon Aug 11 07:13:43 CEST 2008


A record #recname{data1="X",data2="Y",...} is just a tuple of the form
{recname, "X", "Y",...}. Nested records are therefore just nested tuples.

You can dump the contents of any Erlang term using term_to_binary and then
save that binary to disk. You can then restore the contents using
binary_to_term.

()8> R = {rec1,1,2,{rec2,"A","B",{rec3,[1],[2]},"D","E"},3,4,5}.
{rec1,1,2,{rec2,"A","B",{rec3,[1],[2]},"D","E"},3,4,5}
()9> B = term_to_binary(R).
<<131,104,7,100,0,4,114,101,99,49,97,1,97,2,104,6,100,0,4,
  114,101,99,50,107,0,1,65,107,0,...>>
()10> R1 = binary_to_term(B).
{rec1,1,2,{rec2,"A","B",{rec3,[1],[2]},"D","E"},3,4,5}

Making the tuple look like the original record is something I'd like to see,
but I only know of the shell that can do that. The problem is a record is a
compile-time construct; the run-time data does not contains the info (except
I think if compiled in debug mode, which of course you can't guarantee).
Still, it solves your problem from a data point of view.

Hope this helps.

Ed
On Mon, Aug 11, 2008 at 12:34 AM, Vik Olliver <> wrote:

> I have a nested record structure which is populated and then put into an
> mnesia database.
>
> I'd like to be able to recursively extract the structure of the record
> so that I can use it to repopulate another database later (from a
> backup, manual coding, new install or whatever).
>
> Is there a nice way to do this that leaves me with something looking
> very much like the original structure definitions in ASCII?
>
> Vik :v)
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
>


-- 
For every expert there is an equal and opposite expert - Arthur C. Clarke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080811/f7189f4a/attachment.html>


More information about the erlang-questions mailing list