• Save
Python & Perl: Lecture 25
Upcoming SlideShare
Loading in...5
×
 

Python & Perl: Lecture 25

on

  • 832 views

 

Statistics

Views

Total Views
832
Views on SlideShare
625
Embed Views
207

Actions

Likes
0
Downloads
0
Comments
0

9 Embeds 207

http://vkedco.blogspot.com 160
http://www.vkedco.blogspot.com 36
http://vkedco.blogspot.de 3
http://vkedco.blogspot.it 2
http://vkedco.blogspot.com.br 2
http://www.vkedco.blogspot.in 1
http://vkedco.blogspot.in 1
http://vkedco.blogspot.ca 1
http://vkedco.blogspot.fr 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Python & Perl: Lecture 25 Python & Perl: Lecture 25 Presentation Transcript

  • Python & Perl Lecture 25 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  • Outline ● Array References ● Multi-dimensional Arrays ● Sorting Arrays ● Hasheswww.youtube.com/vkedco www.vkedco.blogspot.com
  • Array Referenceswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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
  • Example array_refs_01.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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
  • 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
  • Example array_refs_02.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Multi-dimensional Arrayswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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
  • Example multidim_arrays_01.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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
  • 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
  • Example multidim_arrays_02.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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
  • Sorting Arrayswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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 argumentwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Example sort_01.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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);www.youtube.com/vkedco www.vkedco.blogspot.com
  • Example sort_02.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Hasheswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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 – % - hashwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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 => operatorwww.youtube.com/vkedco
  • Example construct_hash_01.pl construct_hash_02.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 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 definedwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Example manip_hash_01.pl manip_hash_02.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Reading & References ● http://perldoc.perl.org/ ● James Lee. Beginning Perl, 2 nd Edition, APRESS ● Dietel, Dietel, Nieto, McPhie. Perl How to Program, Prentice Hallwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Feedback Bugs, comments to vladimir dot kulyukin at gmail dot comwww.youtube.com/vkedco www.vkedco.blogspot.com