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

Python & Perl: Lecture 23

on

  • 1,262 views

 

Statistics

Views

Total Views
1,262
Views on SlideShare
1,080
Embed Views
182

Actions

Likes
0
Downloads
0
Comments
0

8 Embeds 182

http://vkedco.blogspot.com 143
http://www.vkedco.blogspot.com 25
http://vkedco.blogspot.com.es 4
http://vkedco.blogspot.in 4
http://www.vkedco.blogspot.com.es 2
http://vkedco.blogspot.com.br 2
http://vkedco.blogspot.it 1
http://vkedco.blogspot.com.au 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 23 Python & Perl: Lecture 23 Presentation Transcript

  • Python & Perl Lecture 23 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  • Outline ● Array Creation and Manipulation ● Numeric and String Ranges ● Array Slicing ● Array Functions: push, pop, unshift, shiftwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Array Creation & Manipulationwww.youtube.com/vkedco www.vkedco.blogspot.com
  • List Notation ● A list is a sequence of comma separated elements in a pair of matching parentheses ● () is the empty list ● (1, 2, 3, 4, 5) is a list of four integers 1, 2, 3, 4, 5 ● All Perl lists are one-dimensional (sometimes you may read/hear that all Perl lists are flattened) ● These are the same lists: – (1, 2, 3, 4, 5) – ((1, 2, 3, 4, 5)) – ((1, 2), (3), (4, 5))www.youtube.com/vkedco www.vkedco.blogspot.com
  • Arrays ● Array variables are prefixed with the @ type identifier ● The most straightforward way to create an array is to define an array variable and to assign to it a list of values ● @a1 = ("file1.txt", "file2.txt", "file3.txt"); ● @a2 = ("file1.txt", (("file2.txt")), "file3.txt"); ● @a1 and @a2 are the same, because the lists are flattenedwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Array Creation & Manipulation ● There are four basic ways to create an array in Perl: – Assign a list of values to an array variable – Assign a value to a non-existing element – Use the qw operator – Use the range operatorwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Assigning a List of Values ● @numbers = (1, “one”, 2, “two”, 3, “three”); ● $numbers[0] refers to 1 ● $numbers[1] refers to “one” ● $numbers[2] refers to 2 ● $numbers[3] refers to “two” ● $numbers[4] refers to 3 ● $numbers[5] refers to “three”www.youtube.com/vkedco www.vkedco.blogspot.com
  • Source Code list_assign.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Assign Values to Non-Existing Elements ● When a value is assigned to a non-existing element, the array is automatically created by Perl ● The same concept applies to adding new elements to an array that already exists ● Accessing an array element for which the value has not been provided returns undef ● It is possible to check if an array element is defined using the function definedwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Source Code array_insertion.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Use qw Operator ● The qw (stands for “quoted words”) operator simplifies the creation of lists of strings with no spaces ● qw takes a list of alphanumeric character sequences separated by spaces and converts them into a list of strings ● Each alphanumeric character sequence is converted into a separate stringwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Source Code qw.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Numeric & String Rangeswww.youtube.com/vkedco www.vkedco.blogspot.com
  • Use the range (x .. y) Operator ● The range (x .. y) operator works on numeric and string values ● The operator tries to generate all consecutive values that start at x and end at y by using the increment operator ● Use of this operator is straightforward with numbers but may be tricky with stringswww.youtube.com/vkedco www.vkedco.blogspot.com
  • Numeric Ranges ● @numbers = (1 .. 5); ● 1 is incremented to 2; 2 is incremented to 3; 3 is incremented to 4, etc. ● (1 .. 5) is the same as (1, 2, 3, 4, 5) ● These two statements are equivalent: ● @numbers = (1 .. 5); ● @numbers = (1, 2, 3, 4, 5);www.youtube.com/vkedco www.vkedco.blogspot.com
  • Numeric Ranges ● @numbers = (-5 .. -1); ● The range boundaries can be negative so long as we can get from the left boundary to the right one by increments ● (-5 .. -1) is the same as (-5, -4, -3, -2, -1) ● These two statements are equivalent: ● @numbers = (-5 .. -1); ● @numbers = (-5, -4, -3, -2, -1);www.youtube.com/vkedco www.vkedco.blogspot.com
  • Numeric Ranges ● @numbers = (-1 .. -10); ● If it is impossible to get from the left boundary to the right boundary, the range is empty ● (-1 .. -10) is the same as () ● These two statements are equivalent: ● @numbers = (-1 .. -10); ● @numbers = ();www.youtube.com/vkedco www.vkedco.blogspot.com
  • Numeric Ranges ● @numbers = (1.1 .. 3.1); ● The float boundaries are truncated ● (1.1 .. 3.1) is the same as (1 .. 3) ● These two statements are equivalent: ● @numbers = (1.1 .. 3.1); ● @numbers = (1, 2, 3);www.youtube.com/vkedco www.vkedco.blogspot.com
  • Source Code numeric_ranges.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • String Ranges ● You can define ranges with strings ● @chars = (F .. I); ● F is incremented to G; G to H; H to I ● (F .. I) is the same as (F, G, H, I) ● Mixing numbers and strings in ranges is not allowedwww.youtube.com/vkedco www.vkedco.blogspot.com
  • String Ranges ● When a string is incremented the last character in the string is incremented ● @chars = (ab .. af); ● ab is incremented to ac; ac to ad; ad to ae; ae to af ● (ab .. af) is the same as (ab, ac, ad, ae, af)www.youtube.com/vkedco www.vkedco.blogspot.com
  • String Ranges ● When the last character in the string cannot be incremented, its value is wrapped around and the character to the left is incremented ● @chars = (AZ .. BG); ● AZ becomes BA; BA becomes BB; BB becomes BC, etc. ● (AZ .. BG) is the same as (AZ, BA, BB, BC, BD, BE, BG)www.youtube.com/vkedco www.vkedco.blogspot.com
  • Source Code string_ranges.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Consecutive Array Slicing ● If @a is an array, then @a[x .. y], for x <= y, is a consecutive slice (sub-array) of @a that consists of $a[x], $a[x+1], $a[x+2], …, $a[y] ● If @a is an array, then @a[x .. y], for x > y, is empty ● If @a is an array, then x or y in @a[x .. y] can be negative ● Consecutive slices can be assigned new valueswww.youtube.com/vkedco www.vkedco.blogspot.com
  • Example consec_array_slicing.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Non-consecutive Array Slicing ● If @a is an array, then @a[index_seq] is a non-consecutive slice if index_seq is a comma separated sequence of indexes ● Examples: – @a[1, 4, 5] – @a[5, 1, 4] – @a[-1, 3, 2] ● Non-consecutive array slices can be assigned new valueswww.youtube.com/vkedco www.vkedco.blogspot.com
  • Example nonconsec_array_slicing.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Array Functions push, pop, unshift, shiftwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Array Functions ● push – inserts elements at the end of the array ● pop – removes the last element from the array ● unshift – inserts an element at the beginning of the array ● shift – removes and returns the element at the beginning of the arraywww.youtube.com/vkedco www.vkedco.blogspot.com
  • Example: pop & push ● Display the following pattern of numbers * ** *** **** ***** ***** **** *** ** *www.youtube.com/vkedco www.vkedco.blogspot.com
  • Example: pop & push ● Pseudocode: 1. Use push to push * one at a time at the end of @asterisks 2. Display @asterisks after every push 3. Pop * one at a time from the end of @asterisks 4. Diplay @asterisks after every popwww.youtube.com/vkedco
  • Example push_pop.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Example: shift & unshift ● Display the following pattern of numbers 1 21 321 4321 54321 54321 4321 321 21 1www.youtube.com/vkedco www.vkedco.blogspot.com
  • Example: unshift & shift ● Pseudocode: 1. Use unshift to insert numbers 1 through 5 one at a time at the front of @numbers 2. Print @numbers after every unshift 3. Use shift to remove elements one at a time from the front of @numbers 4. Print @numbers after every shiftwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Source Code unshift_shift.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • Reading & References ● http://perldoc.perl.org/ ● James Lee. Beginning Perl, 2nd Edition, APRESS ● Dietel, Dietel, Nieto, McPhie. Perl How to Program, Prentice Hallwww.youtube.com/vkedco www.vkedco.blogspot.com