Lists of records

julian@REDACTED julian@REDACTED
Mon Jul 16 06:04:46 CEST 2001


Hi All,

I'm new to Erlang, and I'm still trying to get familiar with lists and
records.

I have a (small) list containing records (all of the same type) and I'm
trying to write a function to return a record when supplied with a field
name and value.
I'm happy to assume the field name chosen happens to contain unique values
for now.


I've tried a few things, including the idea below.. but I'm not having much
luck.
Is there a way to do something like this without hardcoding the Fieldname?

Are there any other docs and/or examples on the subject of records apart
from the "Erlang Extensions Since 4.4" doc?



find_record(Fieldname,Searchvalue,[H|T]) ->
    case H of
        #recordtype1{Fieldname=Searchvalue} ->
            H;
        _ ->
           find_record(Fieldname,Searchvalue,T)
    end;
find_record(Fieldname,Searchvalue,[]) ->
    not_found.


The above seems to compile & work only if Fieldname is replaced with an
atom.

I had also hoped to extend this so that the recordtype isn't hardcoded.. any
ideas?


Thanks & Regards,
Julian Noble





More information about the erlang-questions mailing list