[erlang-questions] Iteration over records/record fields ?

igwan <>
Sun Nov 5 14:20:33 CET 2006


Hi,

For a project of mine, I have to import some XML elements into a bunch 
of Mnesia tables. Each record I defined map exactly to the structure of 
each XML element.

Example :

XML :
<person>
  <name>Simpson</name>
  <firstname>Homer J.</firstname>
  ...
</person>

<house>
  <address>742 Evergreen T.</address>
  <coordinates>42.293,12.942,0<coordinates>
  ...
</house>

after parsing and transformation, I get a list like :
[{person, [{name, "Simpson"}, {firstname, "Homer J."}, ... ]},
{house, [{address, "742 Evergreen T."}, {coordinates, 
"42.293,12.942,0"}, ...],
... ]

The corresponding record I define :
-record(person, { name = [], firstname = [], ... }).
-record(house, { address = [], coordinates = [], .... }).
...

The problem is that I have an important list of records defined (about 
30), each having about 20 to 40 fields.
How can I programmatically iterate over my list  to populate my records ?

I wish I could write #RecordName.Field or record_info(fields, 
RecordName), with RecordName and Field being variables, and iterate in 
list comprehensions and lists:foldl but that doesn't seem possible. Is 
there a way around ? (I wish I could also avoid macros. ^^). I also 
heard of the Smerl trickery, is that the way to go ?

Thanks

igwan




More information about the erlang-questions mailing list