Let's say I have this:
-record(my_record, {foo, bar, baz}). Keyvalpairs = [{foo, val1}, {bar, val2}, {baz, val3}]. Foorecord = #my_record{foo=val1, bar=val2, baz=val3}.
How do I convert Keyvalpairs into Foorecord?
Let's say I have this:
-record(my_record, {foo, bar, baz}). Keyvalpairs = [{foo, val1}, {bar, val2}, {baz, val3}]. Foorecord = #my_record{foo=val1, bar=val2, baz=val3}.
How do I convert Keyvalpairs into Foorecord?
Like the other answers point out, you need to roll your own solution to accomplish this. The solutions proposed are however incomplete. For example, it doesn't take into account default values for record entries. I use the following code snippet to take care of this conversion:
Now one can simply do:
which yields
I put this into a little "toolbox" library I am building to collect these little "snippets" that just make life easier when developing Erlang applications: ETBX
If you have the values in the same order as in the record, you can convert directly into the record, you just need to precede the name of the record at the first element of the list and then convert the list into a tuple.
The simplest thing to do is:
If this is too repetitive you can do something like: