Your SlideShare is downloading. ×
Cs3430 lecture 17
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Cs3430 lecture 17

41
views

Published on

Published in: Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
41
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Python & Perl Lecture 17 Department of Computer Science Utah State University
  • 2. Outline ● Array References ● Multi-dimensional Arrays ● Sorting Arrays ● Hashes
  • 3. Array References
  • 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
  • 5. Example array_refs_01.pl
  • 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];
  • 8. Example array_refs_02.pl
  • 9. Multi-dimensional Arrays
  • 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
  • 11. Example multidim_arrays_01.pl
  • 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}
  • 14. Example multidim_arrays_02.pl
  • 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
  • 16. Sorting Arrays
  • 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
  • 18. Example sort_01.pl
  • 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);
  • 20. Example sort_02.pl
  • 21. Hashes
  • 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
  • 24. construct_hash_01.pl construct_hash_02.pl Example
  • 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
  • 26. manip_hash_01.pl manip_hash_02.pl Example
  • 27. Reading & References ● http://perldoc.perl.org/ ● James Lee. Beginning Perl, 2nd Edition, APRESS ● Dietel, Dietel, Nieto, McPhie. Perl How to Program, Prentice Hall