[erlang-questions] Noob alert: tuple to record?

Peter Sabaini peter@REDACTED
Fri Dec 5 00:36:11 CET 2008


On Thursday 04 December 2008 23:19:39 Torben Hoffmann wrote:
> Hi Peter,
>
> This is a painful area.

I gathered as much -- records seem a bit like an afterthought ;-)

Fortunately, for my problem I've got a solution now

> Sadly, you cannot do this automatically. You can do various tricks with the
> internal representation of the records - see
> http://www.erlang.org/doc/reference_manual/records.html#8.7 - but it will
> never be beautiful.
>
> A possible solution:
> use Fields=record_info(fields,<your record name>) to get the list of fields
> in the record and then for each {Name,Value} pair you find out the position
> of the Name in the Fields list and the do a nasty
> setelement(Position,Record,Value) to set the value.
>
> Another question before you dive in: you have a nice list of {Name,Value}
> which matches with the proplists module, perhaps you do not need to put the
> things into a record?

Oh, another module to check out! Joy of learning a new language :-)

I've now butchered something together involving record_info() and the dict 
module, so I'm all set. Basically I create an orddict with all possible field 
values from a record, create a second dict with the actual values, update the 
first with the second and fetch the values; together with the rectype atom 
I've got my record. 

On the other hand I now begin to wonder if I should bother with records at all 
and instead use dicts or proplists for my data?

Thanks!

peter.


> Could you tell us a bit more about what problem you are trying to solve on
> the level above your particular question?
>
> Cheers,
> Torben
>
> On Thu, Dec 4, 2008 at 9:34 PM, Peter Sabaini <peter@REDACTED> wrote:
> > Hello list,
> >
> > Please forgive the noobness: if I have a list of {Name, Value} pairs
> > corresponding to names defined in a record, is there a way to set those
> > values
> > in a record automatically?
> >
> > Thanks,
> > peter.
> > --
> >  Peter Sabaini
> >  http://sabaini.at/
> >
> >
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://www.erlang.org/mailman/listinfo/erlang-questions

-- 
  Peter Sabaini
  http://sabaini.at/
  




More information about the erlang-questions mailing list