পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Tuesday, January 31, 2017

Erlang - Records

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.

Creating a Record

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.

Syntax

record(recordname , {Field1,Field2 ..Fieldn})

Parameters

  • recordname − This is the name given to the record.
  • Field1,Field2 ..Fieldn − These are the list of various fields which constitute the record.

Return Value

None

For example

-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 −

Syntax

#recordname {fieldName1 = value1, fieldName2 = value2 .. fieldNameN = valueN}
Where in you assign values to the respective fields when an instance of the record is defined.

Accessing a Value of the Record

To access the fields and values of a particular record, the following syntax should be used.

Syntax

#recordname.Fieldname

Parameters

  • recordname − This is the name given to the record.
  • Fieldname − This is the name of the field which needs to be accessed.

Return Value

The value assigned to the field.

For example

-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]).

Output

The output of the above program is as follows.
1
“John”

Updating a Value of the Record

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.

Syntax

#recordname.Fieldname = newvalue

Parameters

  • 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

Return Value

The new record with the new values assigned to the fields.

For example

-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]).

Output

The output of the above program is as follows −
1
“Dan”

Nested Records

Erlang also has the facility to have nested records. The following example shows how these nested records can be created.

For example

-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.

Output

The output of the above program is as follows.
1

No comments:

Post a Comment