![]() |
![]() ![]() ![]() |
![]() |
![]() |
||||||
![]() |
|||||||||
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
![]() |
||||||
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Pattern Matching and Regular Expressions If you were paying attention, you noticed a huge loophole in the programs above: there's nothing to prevent you from typing in a string variable when you're supposed to be typing in a number. You can type in "dog" and "cat", and the program will try to add "dog" and "cat" (which, if you're curious, gives a result of zero.) You need some way to check to make sure that the person actually typed in numbers; then, if they didn't, you can ask them again (with a looping control structure), until they get it right. Welcome to the concepts of pattern matching and regular expressions, two of Perl's powerful text-processing tools. Let's start with a simple pattern first: one letter. If you want to test a variable to see if it contains the (lower-case) letter "z", use this syntax: if ($x =~ /z/) { Let's take that apart: if is just like while, except it only checks once (that is, it won't loop around again and again.) Like while, it will execute every command inside the curly brackets if the statement inside the parentheses is true. The statement inside the parentheses works like this: =~ makes a comparison between $x and whatever's inbetween the two slashes; in this case, if there's a z anywhere inside $x, then the statement is true. Let's up the ante, and match only if $x begins with the letter z: if ($x =~ /^z/) { ^z is a regular expression; the carat (^) stands for the beginning of the string. Thus, the matching statement has to find a z immediately following the beginning of the string in order to be true. How about words that begin with z and end with e? Use the regexp /^z.*e$/ The $ stands for the end of the string; the period stands for "any character whatsoever"; combined with the asterisk, it means "zero or more characters." Without the asterisk, /^z.e$/ would mean "z followed by one character followed by e." There's a lot of different regular expressions. For instance, /^z.+e$/ means "z followed by at least one character, followed by e." /^z\w*e$/ means "z followed by zero or more word characters followed by e"--that is, "z!e" wouldn't match. So to make sure that somebody's typing in numbers in our adding program, and not words, make the subroutine getnumber look like this:
"\D" is the regular expression for non-digits; if any character in $number is not 0-9, the expression won't match, and you'll get asked to enter a number again. Note how we had to set $number to include a non-digit ($number = "blah") to get inside the loop the first time around. Substitution You can transform variables at will or whim using regular expressions, by use of the substitution command. This command will change the letters "dog" to "cat" if they occur in the variable $x: $x =~ s/dog/cat/; Actually, that will only change the last occurence (so "dogdog" would become "dogcat".) To make the change "global", add a g at the end: $x =~ s/dog/cat/g; This will change "dig" and "dog" (but not "doug") to "cat": $x =~ s/d.g/cat/g; A Little About Arrays A single number or string can be assigned to a scalar variable, in the form $x = 45; If you have a bunch of variables and want to store them together, that's an array. Here's how you assign the numbers 3, 5, 7, and 9 into an array called @dog: @dog = (3, 5, 7, 9); Although the entire array is referred to as @dog, an individual element--let's say, the first one--is $dog[0]. So $dog[0] equals 3, and $dog[1] equals 5. (Throughout history, programmers have begun their lists with the number zero, and throughout history, this has caused nothing but grief and turmoil.) Note that the variable $dog and the array elements $dog[0]...$dog[3] are totally unrelated. $dog could be "zebra" or 87000, and it would never affect any of the elements of @dog. That's just the barest hint of what arrays can do. I'm only bringing them up now so you won't freak out if I use them in an example.
|
||||||||
![]() |
![]() |
![]() |
|||||||
![]() |
![]() |
![]() |
|||||||
© 2001-2003, Yee-Ting Li, email: ytl@hep.ucl.ac.uk,
Tel: +44 (0) 20 7679 1376, Fax: +44 (0) 20 7679 7145 Room D14, High Energy Particle Physics, Dept. of Physics & Astronomy, UCL, Gower St, London, WC1E 6BT |
|||||||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |