Presentation is loading. Please wait.

Presentation is loading. Please wait.

LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong.

Similar presentations


Presentation on theme: "LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong."— Presentation transcript:

1 LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong

2 Today's Topic Homework 5 Review Perl regex Homework 6: Reading
probably the most valuable part of Perl… Homework 6: Reading Homework 7: Exercises Reminder: no class all next week

3 Homework 5 Brief Review hw5data1.txt hw5data2.txt Discard 1st 3 lines
For every 3: Discard 1st Use 2nd as key 3rd is population Discard 1st 3 lines For every 3: Use 1st as key Discard 2nd 3rd is area

4 Homework 5 Brief Review Use code template: Useful code snippets:
chomp $s; $s =~ tr/,//d; $s =~ s/^\s+|\s+$//g; From lecture 7, let's also use our ability to make a hash table that contains sub-hash tables hw5data1.txt Discard 1st 3 lines For every 3: Discard 1st Use 2nd as key 3rd is population hw5data2.txt Discard 1st 3 lines For every 3: Use 1st as key Discard 2nd 3rd is area

5 Homework 5 Brief Review Get organized, and start with …

6 Homework 6 Read textbook chapter 2: section 1 on Regular Expressions

7 Homework 6 Read up on the syntax of Perl Regular Expressions
Online tutorials

8 Perl Regular Expressions
Perl regex matching: $a =~ /foo/ (/…/ contains a regex) can use in a conditional: evaluates to true/false depending on what’s in $a can also use as a statement: variable $& contains the match Perl regex match and substitute: $a =~ s/foo/bar/ s/…match… /…substitute… / contains two expressions will modify $a by looking for a single occurrence of match and replacing that with substitute s/…match… /…substitute… /g global substitution

9 Perl Regular Expressions
Most useful with the code template for reading in a file line-by-line: open($txtfile,$ARGV[0]) or die "$ARGV[0] not found!\n"; while ($line = <$txtfile>) { do RE stuff with $line }

10 Chapter 2: JM character class: Perl lingo

11 Chapter 2: JM range: in ASCII table
backslash lowercase letter for class Uppercase variant for all but class

12 Chapter 2: JM

13 Chapter 2: JM Sheeptalk

14 Homework 7 Do regex exercises 2.1 in JM (pg. 42)
Ungraded, but please do them… I will review some of them next time …

15 Another example \s is a whitespace, so \S is a non-whitespace
\S+ing\b \s is a whitespace, so \S is a non-whitespace + is repetition (1 or more) \b is a word boundary, (words are made up of \w characters)

16 Another example \b or \b{wb} variables

17 Note: global match in while-loop
Another example \b \b{wb} Note: global match in while-loop

18 Unicode and \w Definition is expanded for Unicode: \w is [0-9A-Za-z_]
use utf8; use open qw(:std :utf8); my $str = "school école École šola trường स्कूल škole โรงเรียน"; @words = ($str =~ /(\w+)/g); foreach $word { print "$word\n" } list context

19 Chapter 2: JM

20 Chapter 2: JM Precedence of operators Perl: Precedence Hierarchy:
Example: Column 1 Column 2 Column 3 … /Column [0-9]+ */ /(Column [0-9]+ *)*/ /house(cat(s|)|)/ Perl: in a regular expression the pattern matched by within the pair of parentheses is stored in designated variables $1 (and $2 and so on) Precedence Hierarchy: space

21 Perl Regular Expressions
returns 1 (true) or "" (empty if false) A shortcut: list context for matching returns a list


Download ppt "LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong."

Similar presentations


Ads by Google