SlideShare a Scribd company logo
Python & Perl
Lecture 17
Department of Computer Science
Utah State University
Outline
●
Array References
●
Multi-dimensional Arrays
●
Sorting Arrays
●
Hashes
Array References
Array References
●
It is possible to get references to arrays
●
An array reference is an address of the first element
●
If @ary is an array, then @ary is a named reference
to @ary
●
Example: $ary0_ref = @ary;
●
You can get multiple named references to the same
array
Example
array_refs_01.pl
Array References
●
If @ary is an array and $ary_ref is a reference to it,
then you can use $ary_ref to access individual
elements of @ary
●
Example:
my @ary = (1 .. 5);
my $ary_ref = @ary0;
$ary_ref->[0] ## refers to 1
$ary_ref->[4] ## referes to 5
Array References
●
You can assign values to references to destructively modify
arrays
●
For example, if $ary0_ref is a reference to and @ary0 and
a reference to it, then you can use $ary1_ref is a reference
to @ary1 the references can be used to assign elements of
one array
●
Here is how you can assign the first value of @ary1 to the
first value of @ary0:
$ary0_ref->[0] = $ary1_ref->[0];
Example
array_refs_02.pl
Multi-dimensional Arrays
Construction & Iteration
●
Multi-dimensional arrays can be constructed with named
and anonymous array references
●
Suppose @row0, @row1, and @row2 are arrays and
$row_ref0, $row_ref1, and $row_ref2 are named
references
●
Then you can construct a 2D array @rslt as
@rslt = ($row_ref0, $row_ref1, row_ref2);
●
$rslt[$r]->[$c] refers to element at r, c
Example
multidim_arrays_01.pl
Anonymous References
●
Using anonymous array references is more
straightforward if you do not need to refer to sub-
arrays by names
●
Example: @ary = ([1, 2, 3], ['a', 'b', 'c'])
constructs a 2x3 array with anonymous references
(i.e., references to [1, 2, 3] and ['a', 'b', 'c'] are
not named)
●
$ary[$r][$c] refers to element at r, c
Iterating over Anonymous References
●
You can use foreach to iterate over the sub-arrays
●
Example: foreach my $aref (@ary) { … }, $aref
iterates over anonymous references to sub-arrays
●
@{$aref} is the sub-array to which $aref currently refers
●
Example: foreach my $e (@{$aref}) { … } iterates over
the scalars in @{$aref}
Example
multidim_arrays_02.pl
Variable Length Rows
●
Rows in multi-dimensional arrays do not have to be of the
same length
●
You can use $#{} notation to obtain the number of
elements in each row
●
Example: if @ary2 is a 2D array, $#{ary2[$r]}+1 is the
length of row $r
●
Example: if @ary3 is a 3D array, $#{ary3[$r][$c]}+1 is
the length of row at $r, $c
Sorting Arrays
Default Sorting Settings
●
The sort function by default sorts its argument array/list
alphanumerically (i.e., it treats array/list elements as
strings)
●
In other words, even if all elements in in the argument
array/list are numbers, they are sorted as strings
●
The sort function returns a sorted copy of its array/list
argument
Example
sort_01.pl
Customized Sorting
●
You can explicitly customize sorting to work on strings and
numbers
●
If you want to have an array/list sorted numerically, use
{ $a <=> $b }
●
If you want to have an array/list sorted alphanumerically,
use { $a cmp $b }
●
Example: sort { $a <=> $b } (5 , 1, 3);
●
Example: sort { $a cmp $b } qw(Perl Python);
Example
sort_02.pl
Hashes
Hashes
●
A hash is a one-to-one mapping from keys to values
●
Keys are not ordered
●
A hash variable must be marked with the % type identifier
●
Three main type identifiers:
 $ - scalar
 @ - array
 % - hash
Hash Construction
●
A hash can be constructed from a list of comma-
separated key-value pairs
●
A hash can be constructed by inserting key-value pairs
into it
●
A hash can be constructed from a list with the =>
operator
construct_hash_01.pl
construct_hash_02.pl
Example
Hash Manipulation
●
Once a hash exists, one can:
 Get the keys
 Get the values
 Iterate through the key-value pairs
 Swap the keys and values
 Check if a key exists or is defined
manip_hash_01.pl
manip_hash_02.pl
Example
Reading & References
●
http://perldoc.perl.org/
●
James Lee. Beginning Perl, 2nd
Edition, APRESS
●
Dietel, Dietel, Nieto, McPhie. Perl How to Program,
Prentice Hall

More Related Content

What's hot

arrays of structures
arrays of structuresarrays of structures
arrays of structures
arushi bhatnagar
 
arrays in c
arrays in carrays in c
arrays in c
vidhi mehta
 
Oracle Collections
Oracle CollectionsOracle Collections
Oracle Collections
Trendz Lab
 
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
monikadeshmane
 
PHP array 2
PHP array 2PHP array 2
PHP array 2
Mudasir Syed
 
Arrays
ArraysArrays
Introduction to array and string
Introduction to array and stringIntroduction to array and string
Introduction to array and string
MuntasirMuhit
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
Amirul Azhar
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
Shahzaib Ajmal
 
Marcs (bio)perl course
Marcs (bio)perl courseMarcs (bio)perl course
Marcs (bio)perl course
BITS
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
2569294Mohan
 
How to Create an Array & types in PHP
How to Create an Array & types in PHP How to Create an Array & types in PHP
How to Create an Array & types in PHP
Ajit Sinha
 
Power shell basics day 5
Power shell basics day 5Power shell basics day 5
Power shell basics day 5
Ashish Raj
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
Nikhil Pandit
 
Array C programming
Array C programmingArray C programming
Array C programming
Prionto Abdullah
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
Terry Yoast
 
学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト
Opt Technologies
 

What's hot (17)

arrays of structures
arrays of structuresarrays of structures
arrays of structures
 
arrays in c
arrays in carrays in c
arrays in c
 
Oracle Collections
Oracle CollectionsOracle Collections
Oracle Collections
 
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
 
PHP array 2
PHP array 2PHP array 2
PHP array 2
 
Arrays
ArraysArrays
Arrays
 
Introduction to array and string
Introduction to array and stringIntroduction to array and string
Introduction to array and string
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
Marcs (bio)perl course
Marcs (bio)perl courseMarcs (bio)perl course
Marcs (bio)perl course
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
How to Create an Array & types in PHP
How to Create an Array & types in PHP How to Create an Array & types in PHP
How to Create an Array & types in PHP
 
Power shell basics day 5
Power shell basics day 5Power shell basics day 5
Power shell basics day 5
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Array C programming
Array C programmingArray C programming
Array C programming
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
 
学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト
 

Similar to Cs3430 lecture 17

Lists and arrays
Lists and arraysLists and arrays
C
CC
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
KristinaBorooah
 
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent. Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Workhorse Computing
 
Scalar data types
Scalar data typesScalar data types
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions
Ahmed El-Arabawy
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
Dave Cross
 
Intermediate Perl
Intermediate PerlIntermediate Perl
Intermediate Perl
Dave Cross
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
sana mateen
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
Eng Teong Cheah
 
Regular expressionfunction
Regular expressionfunctionRegular expressionfunction
Regular expressionfunction
ADARSH BHATT
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
Sharad Dubey
 
Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014
Prof. Wim Van Criekinge
 
arrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdfarrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdf
MarlonMagtibay2
 
PHP-04-Arrays.ppt
PHP-04-Arrays.pptPHP-04-Arrays.ppt
PHP-04-Arrays.ppt
Leandro660423
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
Array
ArrayArray
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
40NehaPagariya
 
First steps in PERL
First steps in PERLFirst steps in PERL
First steps in PERL
Brahma Killampalli
 

Similar to Cs3430 lecture 17 (20)

Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
 
C
CC
C
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent. Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
 
Scalar data types
Scalar data typesScalar data types
Scalar data types
 
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
 
Intermediate Perl
Intermediate PerlIntermediate Perl
Intermediate Perl
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
 
Regular expressionfunction
Regular expressionfunctionRegular expressionfunction
Regular expressionfunction
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014
 
arrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdfarrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdf
 
PHP-04-Arrays.ppt
PHP-04-Arrays.pptPHP-04-Arrays.ppt
PHP-04-Arrays.ppt
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
 
Array
ArrayArray
Array
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
 
First steps in PERL
First steps in PERLFirst steps in PERL
First steps in PERL
 

More from Tanwir Zaman

Cs3430 lecture 15
Cs3430 lecture 15Cs3430 lecture 15
Cs3430 lecture 15
Tanwir Zaman
 
Cs3430 lecture 14
Cs3430 lecture 14Cs3430 lecture 14
Cs3430 lecture 14
Tanwir Zaman
 
Cs3430 lecture 13
Cs3430 lecture 13Cs3430 lecture 13
Cs3430 lecture 13
Tanwir Zaman
 
Cs3430 lecture 16
Cs3430 lecture 16Cs3430 lecture 16
Cs3430 lecture 16
Tanwir Zaman
 
Python lecture 12
Python lecture 12Python lecture 12
Python lecture 12
Tanwir Zaman
 
Python lecture 10
Python lecture 10Python lecture 10
Python lecture 10
Tanwir Zaman
 
Python lecture 09
Python lecture 09Python lecture 09
Python lecture 09
Tanwir Zaman
 
Python lecture 8
Python lecture 8Python lecture 8
Python lecture 8
Tanwir Zaman
 
Python lecture 07
Python lecture 07Python lecture 07
Python lecture 07
Tanwir Zaman
 
Python lecture 06
Python lecture 06Python lecture 06
Python lecture 06
Tanwir Zaman
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
Tanwir Zaman
 
Python lecture 04
Python lecture 04Python lecture 04
Python lecture 04
Tanwir Zaman
 
Python lecture 03
Python lecture 03Python lecture 03
Python lecture 03
Tanwir Zaman
 
Python lecture 02
Python lecture 02Python lecture 02
Python lecture 02
Tanwir Zaman
 
Python lecture 01
Python lecture 01Python lecture 01
Python lecture 01
Tanwir Zaman
 
Python lecture 11
Python lecture 11Python lecture 11
Python lecture 11
Tanwir Zaman
 

More from Tanwir Zaman (16)

Cs3430 lecture 15
Cs3430 lecture 15Cs3430 lecture 15
Cs3430 lecture 15
 
Cs3430 lecture 14
Cs3430 lecture 14Cs3430 lecture 14
Cs3430 lecture 14
 
Cs3430 lecture 13
Cs3430 lecture 13Cs3430 lecture 13
Cs3430 lecture 13
 
Cs3430 lecture 16
Cs3430 lecture 16Cs3430 lecture 16
Cs3430 lecture 16
 
Python lecture 12
Python lecture 12Python lecture 12
Python lecture 12
 
Python lecture 10
Python lecture 10Python lecture 10
Python lecture 10
 
Python lecture 09
Python lecture 09Python lecture 09
Python lecture 09
 
Python lecture 8
Python lecture 8Python lecture 8
Python lecture 8
 
Python lecture 07
Python lecture 07Python lecture 07
Python lecture 07
 
Python lecture 06
Python lecture 06Python lecture 06
Python lecture 06
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
Python lecture 04
Python lecture 04Python lecture 04
Python lecture 04
 
Python lecture 03
Python lecture 03Python lecture 03
Python lecture 03
 
Python lecture 02
Python lecture 02Python lecture 02
Python lecture 02
 
Python lecture 01
Python lecture 01Python lecture 01
Python lecture 01
 
Python lecture 11
Python lecture 11Python lecture 11
Python lecture 11
 

Recently uploaded

How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 

Recently uploaded (20)

How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 

Cs3430 lecture 17

  • 1. Python & Perl Lecture 17 Department of Computer Science Utah State University
  • 4. Array References ● It is possible to get references to arrays ● An array reference is an address of the first element ● If @ary is an array, then @ary is a named reference to @ary ● Example: $ary0_ref = @ary; ● You can get multiple named references to the same array
  • 6. Array References ● If @ary is an array and $ary_ref is a reference to it, then you can use $ary_ref to access individual elements of @ary ● Example: my @ary = (1 .. 5); my $ary_ref = @ary0; $ary_ref->[0] ## refers to 1 $ary_ref->[4] ## referes to 5
  • 7. Array References ● You can assign values to references to destructively modify arrays ● For example, if $ary0_ref is a reference to and @ary0 and a reference to it, then you can use $ary1_ref is a reference to @ary1 the references can be used to assign elements of one array ● Here is how you can assign the first value of @ary1 to the first value of @ary0: $ary0_ref->[0] = $ary1_ref->[0];
  • 10. Construction & Iteration ● Multi-dimensional arrays can be constructed with named and anonymous array references ● Suppose @row0, @row1, and @row2 are arrays and $row_ref0, $row_ref1, and $row_ref2 are named references ● Then you can construct a 2D array @rslt as @rslt = ($row_ref0, $row_ref1, row_ref2); ● $rslt[$r]->[$c] refers to element at r, c
  • 12. Anonymous References ● Using anonymous array references is more straightforward if you do not need to refer to sub- arrays by names ● Example: @ary = ([1, 2, 3], ['a', 'b', 'c']) constructs a 2x3 array with anonymous references (i.e., references to [1, 2, 3] and ['a', 'b', 'c'] are not named) ● $ary[$r][$c] refers to element at r, c
  • 13. Iterating over Anonymous References ● You can use foreach to iterate over the sub-arrays ● Example: foreach my $aref (@ary) { … }, $aref iterates over anonymous references to sub-arrays ● @{$aref} is the sub-array to which $aref currently refers ● Example: foreach my $e (@{$aref}) { … } iterates over the scalars in @{$aref}
  • 15. Variable Length Rows ● Rows in multi-dimensional arrays do not have to be of the same length ● You can use $#{} notation to obtain the number of elements in each row ● Example: if @ary2 is a 2D array, $#{ary2[$r]}+1 is the length of row $r ● Example: if @ary3 is a 3D array, $#{ary3[$r][$c]}+1 is the length of row at $r, $c
  • 17. Default Sorting Settings ● The sort function by default sorts its argument array/list alphanumerically (i.e., it treats array/list elements as strings) ● In other words, even if all elements in in the argument array/list are numbers, they are sorted as strings ● The sort function returns a sorted copy of its array/list argument
  • 19. Customized Sorting ● You can explicitly customize sorting to work on strings and numbers ● If you want to have an array/list sorted numerically, use { $a <=> $b } ● If you want to have an array/list sorted alphanumerically, use { $a cmp $b } ● Example: sort { $a <=> $b } (5 , 1, 3); ● Example: sort { $a cmp $b } qw(Perl Python);
  • 22. Hashes ● A hash is a one-to-one mapping from keys to values ● Keys are not ordered ● A hash variable must be marked with the % type identifier ● Three main type identifiers:  $ - scalar  @ - array  % - hash
  • 23. Hash Construction ● A hash can be constructed from a list of comma- separated key-value pairs ● A hash can be constructed by inserting key-value pairs into it ● A hash can be constructed from a list with the => operator
  • 25. Hash Manipulation ● Once a hash exists, one can:  Get the keys  Get the values  Iterate through the key-value pairs  Swap the keys and values  Check if a key exists or is defined
  • 27. Reading & References ● http://perldoc.perl.org/ ● James Lee. Beginning Perl, 2nd Edition, APRESS ● Dietel, Dietel, Nieto, McPhie. Perl How to Program, Prentice Hall