This time we are going to look at regular expressions using Perl. Introduction t
ID: 3885103 • Letter: T
Question
This time we are going to look at regular expressions using Perl. Introduction to Perl: http://www.perl.com/pub/a/2000/10/begperl1.html Regular expressions in Perl: http://perldoc.perl.org/perlretut.html More complete Perl guides (indexed): http://www.perl.org/books/beginning-perl/ http://www.ebb.org/PickingUpPerl/ Let's see if we can do the same thing in Perl that we did with grep last time. Look at the first of The Federalist Papers at http://odur.let.rug.nl/~usa/D/1776-1800/federalist/fed1.htm . Cut and paste the file so you have it as a Unix text file.
E. All words that contain exactly 3 vowel letters ("a","e","i","o","u"); the letters may come in any order.
F. One extra one that would be challenging to do using grep: find the longest word(s) in the text.
Hint: http://perl.about.com/od/programmingperl/qt/perlsplit.htm
Explanation / Answer
If functions are Perl's verbs, then variables are its nouns. Perl has three types of variables: scalars, arrays and hashes. Think of them as ``things,'' ``lists,'' and ``dictionaries.'' In Perl, all variable names are a punctuation character, a letter or underscore, and one or more alphanumeric characters or underscores.
Scalars are single things. This might be a number or a string. The name of a scalar begins with a dollar sign, such as $i or $abacus. You assign a value to a scalar by telling Perl what it equals, like so:
You don't need to specify whether a scalar is a number or a string. It doesn't matter, because when Perl needs to treat a scalar as a string, it does; when it needs to treat it as a number, it does. The conversion happens automatically. (This is different from many other languages, where strings and numbers are two separate data types.)
If you use a double-quoted string, Perl will insert the value of any scalar variables you name in the string. This is often used to fill in strings on the fly:
The final output from this code is The report is: There are 5 apples..
Numbers in Perl can be manipulated with the usual mathematical operations: addition, multiplication, division and subtraction. (Multiplication and division are indicated in Perl with the * and / symbols, by the way.)
You can also use special operators like ++, --, +=, -=, /= and *=. These manipulate a scalar's value without needing two elements in an equation. Some people like them, some don't. I like the fact that they can make code clearer.
Strings in Perl don't have quite as much flexibility. About the only basic operator that you can use on strings is concatenation, which is a $10 way of saying ``put together.'' The concatenation operator is the period. Concatenation and addition are two different things:
Remember that Perl converts strings to numbers transparently whenever it's needed, so to get the value of $b, the Perl interpreter converted the two strings "8" and "1" to numbers, then added them. The value of $b is the number 9. However, $c used concatenation, so its value is the string "81".
Just remember, the plus sign adds numbers and the period puts strings together.
Arrays are lists of scalars. Array names begin with @. You define arrays by listing their contents in parentheses, separated by commas:
The contents of an array are indexed beginning with 0. (Why not 1? Because. It's a computer thing.) To retrieve the elements of an array, you replace the @ sign with a $ sign, and follow that with the index position of the element you want. (It begins with a dollar sign because you're getting a scalar value.) You can also modify it in place, just like any other scalar.
If an array doesn't exist, by the way, you'll create it when you try to assign a value to one of its elements.
Arrays always return their contents in the same order; if you go through @months from beginning to end, no matter how many times you do it, you'll get back July, August and September in that order. If you want to find the length of an array, use the value $#array_name. This is one less than the number of elements in the array. If the array just doesn't exist or is empty, $#array_name is -1. If you want to resize an array, just change the value of $#array_name.
Hashes are called ``dictionaries'' in some programming languages, and that's what they are: a term and a definition, or in more correct language a key and a value. Each key in a hash has one and only one corresponding value. The name of a hash begins with a percentage sign, like %parents. You define hashes by comma-separated pairs of key and value, like so:
You can fetch any value from a hash by referring to $hashname{key}, or modify it in place just like any other scalar.
If you want to see what keys are in a hash, you can use the keys function with the name of the hash. This returns a list containing all of the keys in the hash. The list isn't always in the same order, though; while we could count on @months to always return July, August, September in that order, keys %days_in_summer might return them in any order whatsoever.
The three types of variables have three separate namespaces. That means that $abacus and @abacus are two different variables, and $abacus[0] (the first element of @abacus) is not the same as $abacus{0} (the value in abacus that has the key 0).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.