CSC 4630 Meeting 17 March 21, 2007
Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday 3/19, was cancelled –Quiz 3, will replace Exam 2 on the calendar, so will occur on Wednesday, March 28 –Exam 2, will replace Quiz 3 on the calendar, so will occur on Monday, April 16
Project Schedule Projects 3 and 4 have been replaced by the team software development project Some short homework assignments may be given in their place.
Daily Scrum Questions for the technical advisor?
Computing News John Backus
Owen’s 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
Owen’s Problem (2) What do we need to solve it in Perl? Read a line from 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.
Owen’s Problem (3) Try 1: Make a one line input file: The quick brown fox Make a Perl program that reads and writes it: while (<>) {
Owen’s 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/
Owen’s 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 = split; Redo your program to handle this and print one word per line.
Owen’s 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 { }. If you don’t specify a loop variable, Perl uses $_
Owen’s Problem (7) Try 6: We need the words in alphabetical order. Perl knows sort as a function on an = does the job. Fix your program so that it prints the words in lexicographic order, one per line.
Owen’s Problem (8) Try 7: Where is the UNIX uniq command when we need it?
Perl 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
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}
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}
Control Structures (4) We have the fourth version also do {s} unless c is equivalent to s; unless (c) {s}
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?
Control Structures (6) foreach $i {s} The variable $i ranges over the values of the array and can be referenced by s foreach {s} Uses the default index variable $_ Check the behavior = (1,3,5,7); foreach $i {$i *=3}