Erlang has the extra facility to create records. These records consist of fields. For example, you can define a personal record which has 2 fields, one is the id and the other is the name field. In Erlang, you can then create various instances of this record to define multiple people with various names and id’s.
Let’s explore how we can work with records.
A record is created using the Record Identifier. In this record identifier, you specify the various fields which constitute the record. The general syntax and example are given below.
record(recordname , {Field1,Field2 ..Fieldn})
recordname − This is the name given to the record.
Field1,Field2 ..Fieldn − These are the list of various fields which constitute the record.
None
-module(helloworld). -export([start/0]). -record(person, {name = "", id}). start() -> P = #person{name="John",id = 1}.
The above example shows the definition of a record with 2 fields, one is the id and the other is the name. Also, a record is constructed in the following way −
#recordname {fieldName1 = value1, fieldName2 = value2 .. fieldNameN = valueN}
Where in you assign values to the respective fields when an instance of the record is defined.
To access the fields and values of a particular record, the following syntax should be used.
#recordname.Fieldname
recordname − This is the name given to the record.
Fieldname − This is the name of the field which needs to be accessed.
The value assigned to the field.
-module(helloworld). -export([start/0]). -record(person, {name = "", id}). start() -> P = #person{name = "John",id = 1}, io:fwrite("~p~n",[P#person.id]), io:fwrite("~p~n",[P#person.name]).
The output of the above program is as follows.
1 “John”
The updation of a record value is done by changing the value to a particular field and then assigning the record to a new variable name. The general syntax and example is given below.
#recordname.Fieldname = newvalue
recordname − This is the name given to the record.
Fieldname − This is the name of the field which needs to be accessed.
newvalue − This is the new value which needs to be assigned to the field.
The new record with the new values assigned to the fields.
-module(helloworld). -export([start/0]). -record(person, {name = "", id}). start() -> P = #person{name = "John",id = 1}, P1 = P#person{name = "Dan"}, io:fwrite("~p~n",[P1#person.id]), io:fwrite("~p~n",[P1#person.name]).
The output of the above program is as follows −
1 “Dan”
Erlang also has the facility to have nested records. The following example shows how these nested records can be created.
-module(helloworld). -export([start/0]). -record(person, {name = "", address}). -record(employee, {person, id}). start() -> P = #employee{person = #person{name = "John",address = "A"},id = 1}, io:fwrite("~p~n",[P#employee.id]).
In the above example the following things need to be noted −
We are first creating a person’s record which has the field values of name and address.
We then define an employee record which has the person as a field and an additional field called id.
The output of the above program is as follows.
1