You could use the following module that contains a parse transform for what you need:<div><br><div><a href="https://github.com/jcomellas/mlapi/blob/master/src/dynarec.erl">https://github.com/jcomellas/mlapi/blob/master/src/dynarec.erl</a></div>
<div><br></div><div>When you include it in a module it will automatically create the functions that you need to access the records and fields defined in your module by name. To include it in the module where you have defined the records you do:</div>
<div><br></div><div>-compile([{parse_transform, dynarec}]).</div><div><br></div><div>The functions that will now be available are:</div><div><br></div><div><div>get_value(field_name, Record) -></div><div> Record#record_name.field_name.</div>
<div><br></div><div>set_value(field_name, Value, Record) when is_record(Record, record_name) -></div><div> Record#record_name{field_name = Value}.</div><div><br></div><div>records() -></div><div> [record_name1, record_name2, ...].</div>
<div><br></div><div>fields(record_name) -></div><div> [field_name1, field_name2, ...].</div><div><br></div><div>new_record(record_name) -></div><div> #record_name{}.</div><div><br></div></div><div>Hope it helps,</div>
<div><br></div><div>Juanjo</div><div><br></div><div><br></div><div><div class="gmail_quote">On Fri, Mar 9, 2012 at 12:20 PM, Max Bourinov <span dir="ltr"><<a href="mailto:bourinov@gmail.com">bourinov@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>Hi Erlangers,</div><div><br></div><div>Is there is a way to find a position number of the field in the record:</div>
<div><br></div><div>1. At compile time (record_info)?</div><div>2. At runtime (I think not).</div><div>
<br></div><div>Motivation:</div><div>I use lists module and its functions like lists:keyfind/3 for dealing with records. I don't want to hardcode number to target certain record's field. I afraid that if I change order of fields in my records my code will be broken (It will be indeed). So, I do believe that it is possible to do something like:</div>
<div><br></div><div>...</div><div>-record(my_rec {</div><div> name, age, gender</div><div>}).</div><div>%</div><div>ListOfRecords = [#my_rec{name = "John"}, #my_rec{name = "Peter"}, #my_rec{name = "Marry"}],</div>
<div>Value = "Peter",</div><div>%</div><div>case lists:keyfind(Value, ?get_filed_number(name, my_rec), ListOfRecords) of</div><div>...</div><div><br></div><div>I hope it is possible to define some sort of makro that will do the job...</div>
<div><br></div><div>Any ideas?</div><br clear="all"><div>Best regards,</div><div>Max</div><br><br>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div></div>