+ All Categories
Home > Education > Cs3430 lecture 17

Cs3430 lecture 17

Date post: 25-May-2015
Category:
Upload: tanwir-zaman
View: 59 times
Download: 0 times
Share this document with a friend
Popular Tags:
27
Python & Perl Lecture 17 Department of Computer Science Utah State University
Transcript
Page 1: Cs3430 lecture 17

Python & Perl

Lecture 17

Department of Computer ScienceUtah State University

Page 2: Cs3430 lecture 17

Outline● Array References● Multi-dimensional Arrays● Sorting Arrays● Hashes

Page 3: Cs3430 lecture 17

Array References

Page 4: Cs3430 lecture 17

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

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

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

Multi-dimensional Arrays

Page 10: Cs3430 lecture 17

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

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

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

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

Sorting Arrays

Page 17: Cs3430 lecture 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

Page 19: Cs3430 lecture 17

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

Hashes

Page 22: Cs3430 lecture 17

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

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 25: Cs3430 lecture 17

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 27: Cs3430 lecture 17

Reading & References● http://perldoc.perl.org/● James Lee. Beginning Perl, 2nd Edition, APRESS● Dietel, Dietel, Nieto, McPhie. Perl How to Program,

Prentice Hall


Recommended