1 / 13

COS 381

COS 381. Day 22. Agenda. Questions?? Resources Source Code Available for examples in Text Book in Blackboard Also @ http://perleybrook.umfk.maine.edu/SourceCode/ In Class Work http://perleybrook.umfk.maine.edu/SourceCode/inclassWork/ Assignment 5 is posted Due April 21 at 11:05 AM

bracha
Download Presentation

COS 381

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COS 381 Day 22

  2. Agenda • Questions?? • Resources • Source Code Available for examples in Text Book in Blackboard • Also @ http://perleybrook.umfk.maine.edu/SourceCode/ • In Class Work • http://perleybrook.umfk.maine.edu/SourceCode/inclassWork/ • Assignment 5 is posted • Due April 21 at 11:05 AM • 3rd and final Capstone progress report due April 25 • Final Capstone presentation • May 5 or May 9 @ 10AM • Continue Discussion on Perl • Perl Documentation • http://perldoc.perl.org/ • Perl Tutorial • http://www.sthomas.net/roberts-perl-tutorial.htm

  3. ETS testing • ETS Field Test in Business for • Wednesday, April 23, in Nadeau 109. • the morning; 9:00 a.m. • afternoon 1:00 PM • Juniors and Seniors in the accredited Professional Management Programs (ecommerce) should take this test. • If a student took the test last year, they can take it again, if they want to.

  4. Chapter 8 The Basics of Perl

  5. 8.9 Basics of Pattern Matching • Perl has powerful pattern matching facilities built in • These have been imitated in a number of other systems • Regular expressions were described in the JavaScript chapter • The m operator indicates a pattern matching • This is used with delimiters like q and qq but the enclosed characters form a pattern • If the delimiter is / then the m is not required • m/ddd-dd-dddd/ is the same as /ddd-dd-dddd/ • A match is indicated by the =~ operator with a string on the left and a pattern on the right returns true if there is a match • A pattern alone is matched by default to $_ • /rabbit/ is the same as $_ =~ /rabbitt/ • The split function can take a pattern as the first argument rather than a character • The pattern specifies the pattern of characters used to split the string apart • @words = split /[ .,]\s*/, $str

  6. 8.9 An Example • Example word_table.pl uses a pattern to split a text into words • A hash table is used to count the frequency of each word • The keys are the words, the corresponding values are the counts of the words in the text • The exists function is used to tell if a word is already entered into the hash • http://perldoc.perl.org/perlre.html

  7. 8.9 Remembering Matches • Parts of a pattern can be parenthesized /(reg_ex)(reg_ex)/ • If the pattern matches a string, the variables $1, $2, … refer to the parts of the string matched by the parenthesized sub-patterns • “4 July 1776” =~ /(\d+) (\w+) (d+)/; • print “$2 $1, $3”  July 4 , 1776 • If a match is successful on a string, three strings are available to give the context of the match • $& is the part that actually matched the pattern • $` is the part of the string before the part that matched • $’ is the part of the string after the part that matched "Tony Gauvin" =~ /au/; print "$& $' $`. \n;«  perl\reg_expession_implict.pl

  8. 8.9 Substitutions • The s operator specifies a substitution • s/pattern/new-string/ • s/ there / their / • The new-string will replace the part of a string matched by the pattern • The =~ operator is used to apply the substitution to a string • If the operator is not used, $_ is operated on by default • $str = “they fixed there car”; • $str =~ s/ there / their /; • print “ $str “; • A g modifier on the substitution causes all substrings matching the pattern to be replaced, otherwise only the first match is changed • If the operator is not used, $_ is operated on by default • $str = “they fixed there car in there garage”; • $str =~ s/ there / their /g; • print “ $str “; • The i modifier cause the pattern match to be case insensitive

  9. 8.9 The Transliterate Operator • This is written tr/char-list1/char-list2/ • When applied to a string it causes each character of the string that appears in the first list to be replaced by the corresponding character in the second list • If the second list is empty, the characters from the first list are deleted from the string • The =~ operator is used to apply the transliteration • If the operator is not used, $_ is operated on by default • $test-str = “I Love Perl Programming”; • tr/P/p/; same as s/P/p/g; • tr/aeiou/i;

  10. 8.10 File Input and Output • http://www.troubleshooters.com/codecorn/littperl/perlfile.htm • To carry out file input and output, a filehandle must be created for each file • The open function is used to create a file handle • The first parameter to open is the name of a file handle • By convention the name is all capital letters • The second parameter to open is a string value naming the file and, optionally, including a character to indicate the mode of opening the file • < indicates open for input (default) • > indicates open for output, deleting the content of an existing file • >> indicates open for output, appending to a file that already exists • Read a file open(DAFILE, “<read.txt”) or die “whoops -$1”; • Write a file open(DAFILE, “>write.txt”); • Append to a file open(DAFILE, “>>append.txt”); • Read and write to a file open(DAFILE, “+>readandwrite.txt”);

  11. 8.10 Input and Output Operations • The print function is used to send output to a filehandle • print OUTHANDLE “data”, “more data”; • Note that there is no comma after the OUTHANDLE • This is important, otherwise the value of the handle will be displayed on the output console • The input operator <> can be used on an input file handle • $next_line = <INFILE>; • The read function reads a number of characters into a given array • read(FILE, $store, 255, 0); • The file, the storage, the # of chars, the offest • The function returns actual number of characters read • The function parameters can indicate that characters are to be stored in the array are somewhere other than at the beginning of the file • The seek function can be used to position the filehandle cursor at a different position in the file • seek(FILE, offset, base~[0,1,2]) • offset is the numbers of linesto move (positive moves toward the EOF) • 0 = beginning of file • 1 =current cursor position • 2 is EOF • Examples • seek(FILE, 0 ,0); • seek(FILE, -1, 1);

  12. 8.11 Example • The example wages.pl illustrates many of the features of Perl • An input file contains lines of data with fields separated by colons • The split function can be used to separate the fields • Pattern matches are used on names • A hash is used to store employees and their salaries • More in class work • Help on assignments

  13. 3 card shuffle • Put 3 cards in order • If card b < card a swap card a and card b • If card c < card b swap card c and card b • If card b < card a swap card a and card b • Print a, b, c

More Related