Programming Perl in UNIX Course Number : CIT 370 Week 2 Prof. Daniel Chen
Introduction n Review and Overviews n Chapter 4 n Summary n Lab n Quiz 1 n Next Week (Week 3)
Topics of Discussion n About Perl Variables n Scalars, Arrays, and Hashes n Reading from STDIN n Array Functions n Hash (Associative Array) Functions n More Hashes
Chapter 4: What’s in a Name n 4.1 About Perl Variables n 4.2 Scalars, Arrays, and Hashes n 4.3 Reading from STDIN n 4.4 Array Functions n 4.5 Hash (Associative Array) Functions n 4.6 More Hashes
4.1 About Perl variables n Types u Scalar, Array, Hashes n Scope and the Package u Package, Global Variables n Naming Conventions u Case Sensitive n Assignment Statements n Quoting Rules u Single quotes u Double Quotes u Back Quotes u Perl’s Alternative Quotes (Table 4.1)
4.2 Scalars, Arrays, and Hashes n Scalar Variables u Assignment u The undef Function n Arrays u Special Scalars and Array Assignment u Accessing Elements u Array Slices u Multi-dimension Arrays – Lists of Lists n Hashes u Accessing Elements u Hash Slices u Hashes of Hashes u Array of Hashes
4.3 Reading From STDIN n Assigning Input to a Scalar Variable n The chop and chomp Functions n The read Function n The getc Function n Assigning Input to an Array n Assigning Input to a Hash
4.4 Array Functions n The chop and chomp Functions (with Lists) n The exists Function n The delete Function n The grep Function n The join Function n The map Function n The pack and unpack Functions n The pop Function
Array Functions n The push Function n The shift Function n The delete Function n The splice Function n The split Function n The sort Function n The reverse Functions n The unshift Function
4.5 Hash (Associative Array) Functions n The keys Function n The values Function n The each Function n The delete Function n The exists Function
4. 6 More Hashes n Loading a Hash from a File n Special Hashes u The %ENV hash u The %SIG Hash u The %INC Hash n Context
Matching a string or character at the beginning of a line with a ^ n using the ^ allows you to match words or characters at the beginning of the line only. n Two examples - ls -l ^d - ^…4XC….
Matching a string or character at the end of a line with a $ n The $ lets us match a string or character at the end of the line n Two examples - trouble$ - jet01$
Matching a single or string or character with * n The * allows us to match repeatedly any number of occurrences of any character or string n Two examples - compu*t (computer, computing, compuuuuuute) * ( , 10133, )
Escaping the meaning of a special character with \ n Special characters ($. “ * [ ] | () \ + ?) n To disable the special character - Using \. - Using \*\.pas (*.pas)
Matching ranges or sets using [] n To match certain specific characters or range of characters use [] n Examples - [0-9] or [a-z] - [A-Za-z] - [A-Za-z0-9] - s[a-zA-Z]t - [Cc]omputer - [^a-zA-Z]
Matching a specific number of occurrences with \{\} n If you want to match only a certain number of occurrences, you can use \{ \} n pattern \{n\} (match n occurrences of the pattern) n pattern \[n,\} (match at least n occurrences of the pattern) n pattern \{n,m\} (match occurrences of the pattern between n and m, where n and m are integer numbers between 0 and 255) n Examples - A\{2\}B (AAB) - A\{4,\}B (AAAAAB) - A\{2,4\}B (2 As or 4As)
Summary n Perl Variables n Scalars, Arrays, and Hashes n Reading from STDIN n Array Functions n Hash Functions n Special Characters
Lab n Examples 4.1 – 4.59 (P 43 – 97) n Homework 2
Quiz 1 n Quiz Date: Next Week n Quiz Time: 11:00 AM - 11:30 AM n Contents: Chapter 1- Chapter 4 n No book, no note, no computer
Next Week n Reading assignment (Textbook chapters 5 and 6)