Download presentation
Presentation is loading. Please wait.
Published byNatalie Cain Modified over 8 years ago
1
CSC 4630 Meeting 17 March 21, 2007
2
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
3
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.
4
Daily Scrum Questions for the technical advisor?
5
Computing News John Backus
6
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
7
Owen’s 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.
8
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 (<>) {
9
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/
10
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 say. @words = split; Redo your program to handle this and print one word per line.
11
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 (@a) { }. If you don’t specify a loop variable, Perl uses $_
12
Owen’s 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.
13
Owen’s Problem (8) Try 7: Where is the UNIX uniq command when we need it?
14
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
15
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}
16
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}
17
Control Structures (4) We have the fourth version also do {s} unless c is equivalent to s; unless (c) {s}
18
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?
19
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}
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.