Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.

Similar presentations


Presentation on theme: "CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List."— Presentation transcript:

1 CSC 4630 Perl 3 adapted from R. E. Beck

2 Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List of white-space delimited strings (ignoring case) with frequency counts ordered first by frequency and then lexicographically by ASCII character order. Format is

3 Problem (2) What do we need to solve it in Perl? Read a line from a file: @a = <>; Read the whole file, line by line, and do something to each line while (<>) {something} Note that each line shows up in the variable $_ so that you don’t have to do an assignment statement if you don’t want to.

4 Problem (3) Try 1: Make a one line input file: The quick brown fox Make a Perl program that reads and writes it: while (<>) {

5 Problem (4) Try 2: Same program, put two lines in the input file. Try 3: Change the upper case to lower case. Perl knows about tr and assumes it works on $_ Syntax is tr/ab/cd/

6 Problem (5) Try 4: Now we need to separate the array containing the current line into individual words. Perl can help with split which takes $_ by default, splits it on whitespace by default, and puts the result where you say. @words = split; Redo your program to handle this and print one word per line.

7 Problem (6) Try 5: Collect the lines in an array and print the array, one word per line. Perl knows the array iteration similar to Awk: foreach (@a) { }. If you don’t specify a loop variable, Perl uses $_

8 Problem (7) Try 6: We need the words in alphabetical order. Perl knows sort as a function on an array. @a = sort(@a) does the job. Fix your program so that it prints the words in lexicographic order, one per line.

9 Problem (8) Try 7: Where is the UNIX uniq command when we need it?

10 Control Structures Sequence: Statements separated or terminated by ; (semicolon) If-Then-Else: if (c) {s1} else {s2} Note that c as a conditional expression is false if its value is the string () or the single character 0 (zero). Otherwise c is true

11 Control Structures (2) Fancier If-Then-Else unless (c) {s} is equivalent to if (c) { } else {s} if (c1) {s1} elsif (c2) {s2} elsif (c3) {s3} … else {sn}

12 Control Structures (3) While-Do: while (c) {s} until (c) {s} is equivalent to while (!c) {s} do {s} while c is equivalent to s; while (c) {s}

13 Control Structures (4) We have the fourth version also do {s} unless c is equivalent to s; unless (c) {s}

14 Control Structures (5) Loops with “counters” for (e; c; i) {s} is equivalent to e; while (c) {s; i} Counter issues: –Loop variable local to loop? –Value of loop variable after loop exit?

15 Control Structures (6) foreach $i (@a) {s} The variable $i ranges over the values of the array (list) @a and can be referenced by s foreach (@a) {s} Uses the default index variable $_ Check the behavior of @a = (1,3,5,7); foreach $i (@a) {$i *=3}


Download ppt "CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List."

Similar presentations


Ads by Google