SlideShare a Scribd company logo
2
Most read
4
Most read
6
Most read
Data structures in perl
By
Sana mateen
References
• A reference is a scalar value that points to a memory location that holds some type of
data. Everything in your Perl program is stored inside your computer's memory.
Therefore, all of your variables and functions are located at some memory location.
References are used to hold the memory addresses.
• When a reference is dereferenced, you retrieve the information referred to by the reference.
• Create References
• It is easy to create a reference for any variable, subroutine or value by prefixing it with a
backslash as follows −
• $scalarref = $foo;
• $arrayref = @ARGV;
• $hashref = %ENV;
• $coderef = &handler;
• $globref = *foo;
• You cannot create a reference on an I/O handle (filehandle or dirhandle) using the backslash
operator but a reference to an anonymous array can be created using the square brackets as
follows −
• $arrayref = [1, 2, ['a', 'b', 'c']];
• Similar way you can create a reference to an anonymous hash using the curly brackets as
follows −
• $hashref = { 'Adam' => 'Eve', 'Clyde' => 'Bonnie', };
• A reference to an anonymous subroutine can be created by using sub without a
subname as follows −
• $coderef = sub { print "Boink!n" };
• Following are some of the advantages of passing the reference to a subroutine, instead of
passing the whole array.
1. If we passed the array to a subroutine, Perl copies the entire array into the @_
variable. When the array is big, this is not an effective method.
2. When we want the original array to be modified by the subroutine, we need to
pass the reference of the array.
3. References plays essential role in constructing complex data structures.
4. We can take the reference of an Anonymous Array into the scalar variable as
shown below.
5. $array_ref = [ 11,2,3,45];
Dereferencing
• Dereferencing returns the value from a
reference point to the location. To
dereference a reference simply use
$, @ or % as prefix of the reference
variable depending on whether the
reference is pointing to a scalar,
array, or hash.
Circular References
• A circular reference occurs when two references contain a reference to each
other. You have to be careful while creating references otherwise a circular
reference can lead to memory leaks. Following is an example −
Arrays of Arrays
• In c a two-dimensional array is
constructed as an array of arrays.
a[0][1];
• But in perl we create an array of
references to anonymous arrays.
@colours=([42,128,244],[24,255,0],[0,1
27,127]);
• The array composer converts each
comma separated list to an anonymous
array in memory and returns a reference
to it.
$colours[0][1]=64;
• Second subscript helps to find from
where the element is
selected(dereferencing).
• A 2d array can be created dynamically
by repeatedly using the push operator
to add a reference to an anonymous
array to the top-level array.
Arrays of Hashes
An array of hashes is useful when you have a bunch of records that you'd like to access
sequentially, and each record itself contains key/value pairs.
Composition of an Array of Hashes
You can create an array of anonymous hashes as follows:
@AoH = ( {
husband => "barney",
wife => "betty",
son => "bamm bamm",
},
{ husband => "george",
wife => "jane",
son => "elroy",
},
{ husband => "homer",
wife => "marge",
son => "bart", }, );
To add another hash to the array, you can simply say:
push @AoH, { husband => "fred", wife => "wilma", daughter => "pebbles" };
Generation of an Array of Hashes
•Here are some techniques for populating an array of hashes. To read from a file with the
following format:
•husband=fred friend=barney
•you could use either of the following two loops:
while ( <> ) {
$rec = {};
for $field ( split ) {
($key, $value) = split /=/, $field;
$rec->{$key} = $value;
}
push @AoH, $rec;
}
while ( <> ) {
push @AoH, { split /[s=]+/ };
}
•If you have a subroutine get_next_pair that returns key/value pairs, you can use it to
stuff @AoH with either of these two loops:
while (@fields = get_next_pair() ) {
push @AoH, { @fields }; } while (<>) { push @AoH, { get_next_pair($_) }; }
You can append new members to an existing hash like so:
$AoH[0]{pet} = "dino"; $AoH[2]{pet} = "santa's little helper";

More Related Content

PPTX
Perl names values and variables
PPT
program flow mechanisms, advanced computer architecture
PPT
Parallel algorithms
PDF
Unit 2
PPT
system interconnect architectures in ACA
PDF
Advanced perl finer points ,pack&amp;unpack,eval,files
PPT
program partitioning and scheduling IN Advanced Computer Architecture
PPTX
System software - macro expansion,nested macro calls
Perl names values and variables
program flow mechanisms, advanced computer architecture
Parallel algorithms
Unit 2
system interconnect architectures in ACA
Advanced perl finer points ,pack&amp;unpack,eval,files
program partitioning and scheduling IN Advanced Computer Architecture
System software - macro expansion,nested macro calls

What's hot (20)

PPTX
Compiler design
PDF
Project control and process instrumentation
PPT
1.Role lexical Analyzer
PPT
basics of compiler design
PPTX
Statistical Software Quality Assurance.pptx
PPTX
Network Layer design Issues.pptx
PPTX
Ch 3 software quality factor
PPT
Open MPI
PPTX
Software project management Software economics
PPT
Fullandparavirtualization.ppt
PPTX
Multivector and multiprocessor
PPT
Coda file system
PPTX
Unit 1-introduction to scripts
PPTX
Recognition-of-tokens
PPTX
halstead software science measures
PPTX
Software Engineering Layered Technology Software Process Framework
PDF
Constructive Cost Model - II (COCOMO-II)
PPTX
Wireless transmission
PPT
Domain name system
Compiler design
Project control and process instrumentation
1.Role lexical Analyzer
basics of compiler design
Statistical Software Quality Assurance.pptx
Network Layer design Issues.pptx
Ch 3 software quality factor
Open MPI
Software project management Software economics
Fullandparavirtualization.ppt
Multivector and multiprocessor
Coda file system
Unit 1-introduction to scripts
Recognition-of-tokens
halstead software science measures
Software Engineering Layered Technology Software Process Framework
Constructive Cost Model - II (COCOMO-II)
Wireless transmission
Domain name system
Ad

Viewers also liked (11)

PPT
Introduction to perl_control structures
PDF
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
PDF
Practical SystemTAP basics: Perl memory profiling
PDF
YAPC::Europe 2008 - Mike Astle - Profiling
PDF
Perl Memory Use 201207 (OUTDATED, see 201209 )
ODP
Advanced Perl Techniques
PPT
Perl tutorial
ODP
Profiling with Devel::NYTProf
PDF
Perl Memory Use 201209
PDF
Perl Memory Use - LPW2013
Introduction to perl_control structures
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
Practical SystemTAP basics: Perl memory profiling
YAPC::Europe 2008 - Mike Astle - Profiling
Perl Memory Use 201207 (OUTDATED, see 201209 )
Advanced Perl Techniques
Perl tutorial
Profiling with Devel::NYTProf
Perl Memory Use 201209
Perl Memory Use - LPW2013
Ad

Similar to Data structure in perl (20)

ODP
Introduction to Perl - Day 2
ODP
Intermediate Perl
PDF
Cs3430 lecture 17
PDF
Perl_Part6
PPTX
Marcs (bio)perl course
PDF
Marc’s (bio)perl course
PDF
Introduction to Perl
PPTX
Array,lists and hashes in perl
PDF
Short Introduction To "perl -d"
ODP
Perl Introduction
PPTX
PERL PROGRAMMING LANGUAGE Basic Introduction
PDF
Perl programming language
PDF
Scripting3
PDF
Learning Perl 6
PDF
perl 6 hands-on tutorial
PPTX
Unit 1-subroutines in perl
PPTX
Subroutines in perl
PDF
Memory unmanglement
PDF
Learning Perl 6 (NPW 2007)
Introduction to Perl - Day 2
Intermediate Perl
Cs3430 lecture 17
Perl_Part6
Marcs (bio)perl course
Marc’s (bio)perl course
Introduction to Perl
Array,lists and hashes in perl
Short Introduction To "perl -d"
Perl Introduction
PERL PROGRAMMING LANGUAGE Basic Introduction
Perl programming language
Scripting3
Learning Perl 6
perl 6 hands-on tutorial
Unit 1-subroutines in perl
Subroutines in perl
Memory unmanglement
Learning Perl 6 (NPW 2007)

More from sana mateen (20)

PPTX
PPTX
PHP Variables and scopes
PPTX
Php intro
PPTX
Php and web forms
PPTX
PPTX
Files in php
PPTX
File upload php
PPTX
Regex posix
PPTX
Encryption in php
PPTX
Authentication methods
PPTX
Xml schema
PPTX
Xml dtd
PPTX
Xml dom
PPTX
PPTX
Intro xml
PPTX
Dom parser
PPTX
Unit 1-uses for scripting languages,web scripting
PPTX
Unit 1-strings,patterns and regular expressions
PPTX
Unit 1-scalar expressions and control structures
PPTX
Unit 1-perl names values and variables
PHP Variables and scopes
Php intro
Php and web forms
Files in php
File upload php
Regex posix
Encryption in php
Authentication methods
Xml schema
Xml dtd
Xml dom
Intro xml
Dom parser
Unit 1-uses for scripting languages,web scripting
Unit 1-strings,patterns and regular expressions
Unit 1-scalar expressions and control structures
Unit 1-perl names values and variables

Recently uploaded (20)

PDF
Top 10 read articles In Managing Information Technology.pdf
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPT
Chapter 6 Design in software Engineeing.ppt
PDF
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PPTX
Road Safety tips for School Kids by a k maurya.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
International Journal of Information Technology Convergence and Services (IJI...
PPTX
TE-AI-Unit VI notes using planning model
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
flutter Launcher Icons, Splash Screens & Fonts
PPTX
Internship_Presentation_Final engineering.pptx
PDF
July 2025: Top 10 Read Articles Advanced Information Technology
Top 10 read articles In Managing Information Technology.pdf
ETO & MEO Certificate of Competency Questions and Answers
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Chapter 6 Design in software Engineeing.ppt
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
Road Safety tips for School Kids by a k maurya.pptx
bas. eng. economics group 4 presentation 1.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Lesson 3_Tessellation.pptx finite Mathematics
International Journal of Information Technology Convergence and Services (IJI...
TE-AI-Unit VI notes using planning model
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
flutter Launcher Icons, Splash Screens & Fonts
Internship_Presentation_Final engineering.pptx
July 2025: Top 10 Read Articles Advanced Information Technology

Data structure in perl

  • 1. Data structures in perl By Sana mateen
  • 2. References • A reference is a scalar value that points to a memory location that holds some type of data. Everything in your Perl program is stored inside your computer's memory. Therefore, all of your variables and functions are located at some memory location. References are used to hold the memory addresses. • When a reference is dereferenced, you retrieve the information referred to by the reference. • Create References • It is easy to create a reference for any variable, subroutine or value by prefixing it with a backslash as follows − • $scalarref = $foo; • $arrayref = @ARGV; • $hashref = %ENV; • $coderef = &handler; • $globref = *foo; • You cannot create a reference on an I/O handle (filehandle or dirhandle) using the backslash operator but a reference to an anonymous array can be created using the square brackets as follows − • $arrayref = [1, 2, ['a', 'b', 'c']];
  • 3. • Similar way you can create a reference to an anonymous hash using the curly brackets as follows − • $hashref = { 'Adam' => 'Eve', 'Clyde' => 'Bonnie', }; • A reference to an anonymous subroutine can be created by using sub without a subname as follows − • $coderef = sub { print "Boink!n" }; • Following are some of the advantages of passing the reference to a subroutine, instead of passing the whole array. 1. If we passed the array to a subroutine, Perl copies the entire array into the @_ variable. When the array is big, this is not an effective method. 2. When we want the original array to be modified by the subroutine, we need to pass the reference of the array. 3. References plays essential role in constructing complex data structures. 4. We can take the reference of an Anonymous Array into the scalar variable as shown below. 5. $array_ref = [ 11,2,3,45];
  • 4. Dereferencing • Dereferencing returns the value from a reference point to the location. To dereference a reference simply use $, @ or % as prefix of the reference variable depending on whether the reference is pointing to a scalar, array, or hash.
  • 5. Circular References • A circular reference occurs when two references contain a reference to each other. You have to be careful while creating references otherwise a circular reference can lead to memory leaks. Following is an example −
  • 6. Arrays of Arrays • In c a two-dimensional array is constructed as an array of arrays. a[0][1]; • But in perl we create an array of references to anonymous arrays. @colours=([42,128,244],[24,255,0],[0,1 27,127]); • The array composer converts each comma separated list to an anonymous array in memory and returns a reference to it. $colours[0][1]=64; • Second subscript helps to find from where the element is selected(dereferencing). • A 2d array can be created dynamically by repeatedly using the push operator to add a reference to an anonymous array to the top-level array.
  • 7. Arrays of Hashes An array of hashes is useful when you have a bunch of records that you'd like to access sequentially, and each record itself contains key/value pairs. Composition of an Array of Hashes You can create an array of anonymous hashes as follows: @AoH = ( { husband => "barney", wife => "betty", son => "bamm bamm", }, { husband => "george", wife => "jane", son => "elroy", }, { husband => "homer", wife => "marge", son => "bart", }, ); To add another hash to the array, you can simply say: push @AoH, { husband => "fred", wife => "wilma", daughter => "pebbles" };
  • 8. Generation of an Array of Hashes •Here are some techniques for populating an array of hashes. To read from a file with the following format: •husband=fred friend=barney •you could use either of the following two loops: while ( <> ) { $rec = {}; for $field ( split ) { ($key, $value) = split /=/, $field; $rec->{$key} = $value; } push @AoH, $rec; } while ( <> ) { push @AoH, { split /[s=]+/ }; } •If you have a subroutine get_next_pair that returns key/value pairs, you can use it to stuff @AoH with either of these two loops: while (@fields = get_next_pair() ) { push @AoH, { @fields }; } while (<>) { push @AoH, { get_next_pair($_) }; } You can append new members to an existing hash like so: $AoH[0]{pet} = "dino"; $AoH[2]{pet} = "santa's little helper";