Upcoming SlideShare
×

# Python & Perl: Lecture 23

1,223 views
1,185 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
1,223
On SlideShare
0
From Embeds
0
Number of Embeds
183
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Python & Perl: Lecture 23

1. 1. Python & Perl Lecture 23 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
2. 2. Outline ● Array Creation and Manipulation ● Numeric and String Ranges ● Array Slicing ● Array Functions: push, pop, unshift, shiftwww.youtube.com/vkedco www.vkedco.blogspot.com
3. 3. Array Creation & Manipulationwww.youtube.com/vkedco www.vkedco.blogspot.com
4. 4. 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
5. 5. 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
6. 6. 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
7. 7. 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
8. 8. Source Code list_assign.plwww.youtube.com/vkedco www.vkedco.blogspot.com
9. 9. 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
10. 10. Source Code array_insertion.plwww.youtube.com/vkedco www.vkedco.blogspot.com
11. 11. 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
12. 12. Source Code qw.plwww.youtube.com/vkedco www.vkedco.blogspot.com
13. 13. Numeric & String Rangeswww.youtube.com/vkedco www.vkedco.blogspot.com
14. 14. 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
15. 15. 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
16. 16. 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
17. 17. 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
18. 18. 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
19. 19. Source Code numeric_ranges.plwww.youtube.com/vkedco www.vkedco.blogspot.com
20. 20. 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
21. 21. 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
22. 22. 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
23. 23. Source Code string_ranges.plwww.youtube.com/vkedco www.vkedco.blogspot.com
24. 24. 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