Week 2 Slides

Download Report

Transcript Week 2 Slides

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