cs3430 lecture 17
TRANSCRIPT
![Page 1: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/1.jpg)
Python & Perl
Lecture 17
Department of Computer ScienceUtah State University
![Page 2: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/2.jpg)
Outline● Array References● Multi-dimensional Arrays● Sorting Arrays● Hashes
![Page 3: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/3.jpg)
Array References
![Page 4: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/4.jpg)
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
![Page 6: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/6.jpg)
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
![Page 7: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/7.jpg)
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];
![Page 9: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/9.jpg)
Multi-dimensional Arrays
![Page 10: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/10.jpg)
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
![Page 12: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/12.jpg)
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
![Page 13: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/13.jpg)
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}
![Page 15: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/15.jpg)
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
![Page 16: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/16.jpg)
Sorting Arrays
![Page 17: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/17.jpg)
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
![Page 19: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/19.jpg)
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);
![Page 21: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/21.jpg)
Hashes
![Page 22: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/22.jpg)
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
![Page 23: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/23.jpg)
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
![Page 24: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/24.jpg)
construct_hash_01.plconstruct_hash_02.pl
Example
![Page 25: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/25.jpg)
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
![Page 26: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/26.jpg)
manip_hash_01.plmanip_hash_02.pl
Example
![Page 27: Cs3430 lecture 17](https://reader034.vdocument.in/reader034/viewer/2022042714/55632af8d8b42ad7398b4f30/html5/thumbnails/27.jpg)
Reading & References● http://perldoc.perl.org/● James Lee. Beginning Perl, 2nd Edition, APRESS● Dietel, Dietel, Nieto, McPhie. Perl How to Program,
Prentice Hall