Upcoming SlideShare
×

# Python & Perl: Lecture 25

771 views
698 views

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
771
On SlideShare
0
From Embeds
0
Number of Embeds
217
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Python & Perl: Lecture 25

1. 1. Python & Perl Lecture 25 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
2. 2. Outline ● Array References ● Multi-dimensional Arrays ● Sorting Arrays ● Hasheswww.youtube.com/vkedco www.vkedco.blogspot.com
4. 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 arraywww.youtube.com/vkedco www.vkedco.blogspot.com
6. 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 5www.youtube.com/vkedco www.vkedco.blogspot.com
7. 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 assignment 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];www.youtube.com/vkedco www.vkedco.blogspot.com
10. 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, cwww.youtube.com/vkedco www.vkedco.blogspot.com
12. 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, cwww.youtube.com/vkedco www.vkedco.blogspot.com
13. 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}www.youtube.com/vkedco www.vkedco.blogspot.com
15. 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, \$cwww.youtube.com/vkedco www.vkedco.blogspot.com