Date post: | 25-May-2015 |
Category: |
Education |
Upload: | tanwir-zaman |
View: | 59 times |
Download: | 0 times |
Python & Perl
Lecture 17
Department of Computer ScienceUtah 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
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];
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
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}
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
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);
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.plconstruct_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.plmanip_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