Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Today’s Topics Questions / comments? Anyone try the tutorial? I highly recommend you do if you haven't. Perl –Exercise (my old class solution)‏ –Regular expressions

3 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Modifiers are characters that go after the second forward slash i is a modifier for ignore case. The behaviour for no modifier (the default) is that. Matches any non-newline character ^ matches at beginning of string $ matches at end of string (or before a newline at end)‏ s modifier: treats the string as a single long line, so. matches any character including newline m modifier: treats string as multiple lines so, ^ and $ match the beginning or end of any line But now, \A matches the beginning of the whole string, \Z matches the end of the whole string. Let’s look at this webpage’s examples under Using Character Classes for some more examples: http://www.cs.rit.edu/~afb/20013/plc/perl5/doc/perlretut.html

4 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 | alternation character (acts sort of like a logical or)‏ Grouping characters using the parentheses Getting the “submatches” by using the $1, $2, $3, etc. variables which are set via the parentheses. Repetitions ? - 0 or 1 time * - 0 or more times + - 1 or more times { } – min and max, at least or exactly { min, max } - match >=min times and at most max times. { min, } - match >=min times { n} - match n times exactly

5 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Curly braces { } – min and max, at least or exactly { min, max } - match >=min times and at most max times. { 5, 10 } - matches between 5 and 10 times inclusive { min, } - match >=min times {3, } - matches 3 or more times { n} - match n times exactly { 6 } - matches exactly 6 times Examples of a repetition quantier after a grouping and after a character m/(the){3}/ this will match thethethe all consecutively. m/the{3}/ this will match theee (only the e is repeated 3 times)‏ m/the.*the.*the/ This will match 3 the’s with any characters (except \n) btwn them Any other way to write it?

6 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 In terms of regular expression repetition quantifiers, what does greedy mean again?

7 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 In terms of regular expression repetition quantifiers, what does greedy mean? A quantifier is greedy if it matches as much of the string as possible while still allowing the whole regular expression to match. We'll see that greediness in action now. Let’s continue looking at this site for examples of matching repetitions and the 4 principles that are followed: http://www.cs.rit.edu/~afb/20013/plc/perl5/doc/perlretut.html

8 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Recap on the special variables we learned $_ $` $& and $' (left, match, right)‏ $0 (program name)‏ $1, $2, $3,... (the submatches)‏

9 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Let's write a few regular expressions. match any signed or unsigned integers of arbitrary length. e.g. it should match –-22 –4567 –1 –+43 but not things like: –- –+ –4.56 –abcd –etc.

10 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Let's try these: 1) ignore beginning whitespace if there is any, and match the word program and store the rest of the string (after the word program) into some variable. 2) Now what if there were \n's in the string? What might we change? 3) cs330 or cs106 or CS106 or CS330 but not Cs330, or cS106 etc.

11 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 1) ignore beginning whitespace if there is any, and match the word program and store the rest of the string (after the word program) into some variable. m/\s*program(.*)/ 2) Now what if there were \n's in the string? What might we change? m/\s*program(.*)/s 3) cs330 or cs106 or CS106 or CS330 but not Cs330, or cS106 etc. m/cs330|cs106|CS330|CS106/ OR m/(cs|CS)(106|330)/

12 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Let’s look at a larger parsing example using many of the features we just learned. We'll read the problem and try to solve it ourselves before looking at the solution. The “doing string selections” section of: http://www.troubleshooters.com/codecorn/littperl/perlreg.htm The following page is a good page for reference. It is a nice summary of the different characters and their meanings with succinct examples: http://www.cs.tut.fi/~jkorpela/perl/regexp.html

13 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Declaring and defining subroutines sub name { # code goes here… } Call subroutines &name; &name( parm1, parm2, parm3); # any number of parameters allowed separated by commas name(parm1, parm2, parm3); # the ampersand is optional if using parameters in parentheses.

14 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Parameters in subroutines are stored in a parameter array @_ The value returned from a subroutine is the value of the last evaluation done inside it. Be aware of this! Or you can explicitly use the return function to return a value and terminate the subroutine. Local variables in subroutines are declared by using the my keyword –When declaring more than one, you must put them in parentheses or else all but the first one will be considered global. –my ($parm1, $parm2); When calling the subroutine, the arguments passed in are put in the @_ special array for that subroutine.

15 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 There are a multitude of ways to get at the contents of the elements in the parameter array @_ One of which is $_[0], $_[1], etc... Note well: These have NOTHING to do with the $_ special default variable which we have seen. Another way is to use shift. Shift will shift in the next array element into the variable on the LHS and remove that element from the array. e.g. my $filehand = shift; note: shift works for any array, not just the paramater array. e.g. shift @myarray Another way would be to do something like: my ($age, $height, $weight) = @_;


Download ppt "CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann."

Similar presentations


Ads by Google