Lists of records

Hakan Stenholm etxhste@REDACTED
Mon Jul 16 12:07:24 CEST 2001


You can use lists:keysearch/3 like this


%% The internal representation of a record is a tuple, it is therefore 
%% possible to use all functions that work on tuples on records as well
%% use the #RecordName.FieldName construct rather than hardcoding the value
%% this ensures that it will still work if you change the record, i.e. you
%% don't need to know anything about a record is represented as a tuple

find_record(RecordName,Fieldname,Searchvalue,List) ->
    Pos = #myrecord.myfieldname,		%% this is the records field
    						%% pos in the tuple
    case lists:keysearch(Searchvalue,Pos,List) of
    	{value,Rec} -> Rec;
    	_ -> not_found
    end.

In #RecordName.FieldName the field name must be a atom (acording to the 
documentation) this is most likely due to that records are simply syntactic 
suger that is converted to element/2 and setelement/3 during the preprocessor 
phase in the compiler.

See the text below (copied from the documentation), it might be possible to use
record_info(fields, Rec) to get the position of each field (pos_in_list +1): 


1.9 Internal Representation of Records

It is often desirable to write generic functions which will work on any record, 
not just a record of a particular type. For this reason,
records are represented internally as tuples and the ordering of the fields in 
the tuple is strictly defined.

For example, the record -record(person, {name, phone, address}). is represented 
internally by the tuple {person, X,Y, Z}.

The arity of the tuple is one more than the number of fields in the tuple. The 
first element of the tuple is the name of the record, and the
elements of the tuple are the fields in the record. The variables X, Y and Z 
will store the data contained in the record fields.

The following two functions determine the indices in the tuple which refer to 
the named fields in the record: 

* record_info(fields, Rec) -> [Names]. This function returns the names of the 
  fields in the record Rec. For example,record_info(fields, person) evaluates to 
  [name, address, phone]. 
* record_info(size, Rec) -> Size. This function returns the size of the record 
  Rec when represented as a tuple, which is
  one more than the number of fields. For example, record_info(size, person) 
  returns 4.

In addition, #Rec.Name returns the index in the tuple representation of Name of 
the record Rec. 


                ->>> Name must be an atom.


For example, the following test function test() might return the result shown: 

      test() ->
          {record_info(fields, person),
           record_info(size, person),
           #person.name}.

      > Mod:test().
      {[name,address,phone],4,2}

The order in which records map onto tuples is implementation dependent. 


                ->>> record_info is a pseudo-function which cannot be exported 
                     from the module where it occurs.






More information about the erlang-questions mailing list