English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Erlang has additional features for creating records. These records are composed of fields. For example, you can define a personal record that contains2fields, one is id, and the other is the name field. Then, in Erlang, you can create various instances of the record to define multiple people with different names and IDs.
Let's explore how to handle records.
To create a record, use a record identifier to specify the individual fields that make up the record. The following provides the general syntax and an example.
record(recordname, {Field1,Field2 ..Fieldn)
recordname −This is the name of the record.
Field1,Field2 ..Fieldn −These are the lists of individual fields that make up the record.
none
-module(helloworld). -export([start/0]). -record(person, {name = "", id}). start() -> P = #person{name="John",id = 1}.
The above example shows a record with2a record definition with two fields, one is id, and the other is the name field. In addition, records are constructed in the following way-
#recordname {fieldName1 = value1, fieldName2 = value2 ..fieldNameN = valueN}
When defining a record instance, you can assign values to each field in it.
To access the field and value of a specific record, use the following syntax.
#recordname.Fieldname
recordname −This is the name of the record.
Fieldname −This is the name of the field 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"
To update the value of a record, complete the record value update by assigning the value to a specific field and then assigning the record to a new variable name. The following provides the general syntax and an example.
#recordname.Fieldname = newvalue
recordname −This is the name of the record.
Fieldname −This is the name of the field to be accessed.
newvalue −This is the new value to be assigned to the field.
A new record with a new value assigned to the field.
-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 feature of nested records. The following example explains how to create these nested records.
-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 points should be noted-
We first create a record for a person, with the field values of name and address.
Then we define an employee record, where person is a field, and there is also a field named id.
The output of the above program is as follows.
1