CSCI 344
File I/O in Perl
Lab 11
Write the following Perl scripts. You may use the Internet as a
reference for Perl and as a reference for UNIX/Linux
commands, but you may not search for
solutions to these problems--that would completely defeat the purpose
of doing them.
Here is a link to a good intro to Perl: perldoc.perl.org/perlintro.html
You may NOT use any UNIX/Linux command or utility to solve these problems. They can all be solved with basic Perl.
1) (weight 3) Write a Perl
script to determine if a name is more commonly a girl's name or a boy's
name. Indicate if it is ever a last name. Use the data
files from the US Census in the following directory:
http://www.ecst.csuchico.edu/~tyson/classes/344.s07/data
This directory is available on the Departments file server at:
~tyson/344/data
The files female, male, and last contain a ranking for each name:
MARY 1
PATRICIA 2
LINDA 3
BARBARA 4
ELIZABETH 5
JENNIFER 6
MARIA 7
SUSAN 8
MARGARET 9
For example, MARY is the most common name and is thus ranked 1.
You may design the interface of your program as you see fit. My program works like this:
$ common
What is your name: pat
Pat is more commonly a boy's name
Pat is a last name
What is your name: sally
Sally is a girl's name, but not a boy's name
Sally is a last name
What is your name: patel
Patel is a neither a boy's or a girl's name
Patel is a last name
What is your name:
$
The function split splits the "fields" in a string into an array:
@array = split / /, $string;
@array will contain an element for each space separated field in $string (the delimiter is specified in the / /).
The function uc makes all the characters in it's argument upper case, lc makes them all lower case, and ucfirst capitalizes the first character.
2) (weight 3) Using the name data in problem 1, write a program
that generates random names. That is, a first name randomly
selected from the male and female names and a last name randomly
selected from the last names. If no command line argument is
given, generate 10 names. If a command line argument is given,
assume it is an integer and generate that many names.
$ gen_name 3
Modesta Parlee
Cathryn Polk
Kirby Justice
$ gen_name 3
Allen Kieger
Elmira Wilkes
Dick Mascia
$
The function rand returns a random fractional number greater than or equal to 0 and less
than the value of its argument. You can turn it into an integer using the int function.
3) (weight 3) Write a Perl script that allows the user to lookup a
person in a database using a social security number. Use the file
database.csv as input. Create an interface similar to the
following:
$ db
Enter SS number: 925-79-3341
PAM PAYANO
666 TELSTAR CIRCLE,
CHICO,SC
Enter SS number:
$
As an exercise in using hashes, create a separate hash for each field (first name, last name, street, city, state).