Please place your solutions in the designated files. Your solution to problem
3 should be placed in problem3.txt.
- Write a Perl file named exp.pl that contains a
function that computes the value of ex by using the
ex = 1 + (x/1!) + (x2/2!) + (x3/3!) + ...
The function should take x and the number of terms to use in the
computation. If n is the number of terms then the last term used
in the computation should be xn/n!. The function's
signature should be:
The file should take x and n as command line arguments and
print the result. For example:
bvz% perl exp.pl 1 10
Hint: use e, and not exp as
your function name. exp is a pre-defined Perl function
and perl will get confused if you try to call your function exp.
- Write a Perl function named reverse_array
that takes a reference to an array and returns a new array with
the elements reversed. Requirements for the problem are
Here is a sample output:
- Put the function in a file named reverse.pl.
- Use the command line arguments as the elements of the array to be reversed.
You can simply pass a reference to the command line array
- You may not use Perl's reverse function.
- After the function returns print the reversed array as a comma delimited list
- You must use the join function in conjunction with print to produce the
comma delimited list.
- Hint: You can reverse an array by reading the elements of the
array in order and then pushing (i.e., unshifting) them
onto the front of the new array
UNIX> perl reverse.pl 1 10 20
20, 10, 1
- Write regular expressions to perform each of the following tasks (assume that in each
case you are checking against a string):
- Check that a person entered either 'm' or 'f' for a gender field
- Check that a zip code is between 00000 and 99999
- Check that a date is in the form mm-dd-yyyy with the month, day,
and year all being integers and the year starting with either the prefix
19 or 20. The month and day can be either one or two digits.
- Check that a phone number has one of the following three forms:
- 8651929693 (10 digits with no delimiters)
- (865) 192-2963 or (865)192-2963 (a parenthesized group
of three digits,
followed by an optional space, then three digits, then
a dash, and then four more digits)
- 865-192-2963 (a group of three digits, then a dash, then a
group of three digits, then a dash, then a group of
- Check that an email address is of the form firstname.lastname@example.org...pathn where
name and the paths can be
any combination of letters, numbers, and underscores (_). There can be any
number of '.' separated paths, although there must be at least one,
and the name must be separated from the path
by an '@' sign (warning: any strings you use to test your
pattern should either use single quotes like 'email@example.com' or
else escape the @ character, like "bvz\@cs.utk.edu". If you do
not do heed this warning, then Perl will treat @cs as an interpolated
array and will think the string is really "bvz.utk.edu".)
- Write a Perl function named credit
that takes a string with a credit card number as input and
returns the 4 groups of 4 numbers in an array. You should assume
that the number comes in a string and that the groups of 4 numbers
may have spaces or dashes (-) between them. It is not necessary however
for a group of 4 numbers to have a separator, nor is it necessary to use
the same separator in the group. For example
'1234 5689-39694858' is valid. If the string is invalid
your function should print an error message along with the offending
string. Place your function in a file named credit-card.pl. The
script should assume that it receives a single command line argument.
It should pass this
command line argument to credit and then print out
each of the four groups on a separate line. You may assume that you
always have the correct number of command-line arguments.
Here is a possible execution of credit-card.pl:
perl credit-card.pl "6868-3868 54545868"
Note that you must enclose a command line argument in quotes if
if has one or more white space delimiters.
- Write a Perl script named long.pl
that takes a latitudinal or longitudinal string of the form
"DDD.MM.SS.S" and prints it in the form:
Notice that "degrees", "minutes", and "seconds" are left-aligned. The numbers should
be right-aligned. For example, the string "050.03.22.3" should be printed as:
It is permissable
for the string to omit minutes, seconds, or fractional seconds, in which case the
value should default to 0. For example, the strings "050", "050.02", "050.02",
and "050.02.16" are all valid. You do not need to write a function to implement
- Go to the following web-site and do parts 3-5 with the pgm files. Here are
a few tips/comments:
- Ignore the references to C++/C (e.g., ignore references to malloc)
in the problem description.
- The pgm
files and executables are now in /home/bvz/courses/140/fall-2008/labs/lab2,
not the directory listed on the web-site.
- Your perl scripts should be named
neg.pl, hflip.pl, and vflip.pl.
- If you use split to split the rows of pixels into individual pixel
fields, remember to first strip away any leading whitespace, otherwise
you may encounter the problem with leading empty fields that was
discussed in class.
- Write a program named word_count.pl
to count the frequency of selected words in standard input (STDIN) and
print the words in descending order by word frequency. Words with equal
frequency should be printed in alphabetical order. The selected words
will be input on the command line. For example, given the
The quick brown fox jumped
the brown fence and then
met joey fox for dinner.
and the command:
perl word_count.pl the fox joey dinner < fox.txt
your script would output:
Your program should ignore case (note that "The" and "the" gets the
word frequency for "the" to 2) and should strip non-alphanumeric characters
(alphanumeric characters are upper/lowercase letters and the numbers 0-9)
from the ends of the strings (hence dinner shows up as 1 in the output even
though it has a period attached to it in the input).