117 Homework Assignment #9 (20 pts)
We have studied the usa citypop file (city population).
You are to put a copy of that file in the directory where you will write
this program.
If on our class unix system, use cp like this:
cp /classes/s2007fal/data/citypop/usa .
If you are on windows,
get the file via ftp like this
ftp unix01.sac.edu
enter your 3 char login and 5 char password
cd /classes/s2007fal/data/citypop
get usa
reminder: I gave you an ftp example in hmwk 7.
Write a perl program named: proj9.pl
which should do the following:
- open the usa file from your perl program for reading.
give an error and end the program if you can't open the file.
hint: use the open or die syntax.
- read all lines from the file
and count how many cities are listed for each state.
At the end of the program,
list all the states in alphabetical order
and the number of cities in that state.
Also list the average number of cities per state,
and which state has the most cities (maximum)
and which state has the fewest cities (minimum).
If multiple states are tied
for having the most or fewest cities,
you only need to list one of them.
If you finish early
and need more of a challenge,
list all the states that tie
for having most of fewest cities.
- include all lines of the file (except the first header line)
even if the state is not one of the 50 states.
- use a hash where the key is the 2 letter state
and the value of that key is the number of cities
seen in that state so far.
Each time you see a new line,
check if that state is defined in the hash.
If yes, just ++ the value of that key.
If not, add the key with the value 1.
- then output your 3 char login
and the program name as the last lines of output.
On your PC,
press PrintScreen to capture this last page of output
copy the screen image to word pad
and save it as proj9.rtf
hint: start => run => wordpad
- to get credit for this assignment
upload your program and screen image via ftp
to unix01.sac.edu
in directory /cs117/xxx
where xxx is your 3 char login.
- hints:
develop this program in stages!
stage 1:
read all the lines of the usa file and display.
Make sure your program runs each stage correctly
before going on to work on the next stage!
stage 2:
read usa file and print just the 2 letter state in that line
(instead of the whole line)
stage 3:
add this:
check if the state is defined in the hash,
if so, ++ the value
else add the state and the value 1 to the hash.
at the end, print the states in sorted order
with the correct count
hint: there are 62 cities in CA
Hint: see p 145, lines 18-20
in those lines,
$_ is the key
$hash{ $_ } is the value of that key
stage 4:
add code to count the number of cities and the number of states.
You may either count them when adding data to the hash
or when displaying the number of cities per state.
Calculate and display the average # of cities per state.
stage 5:
As you add data to the hash,
keep track of which state has the most cities.
start of the program:
$MostCities = 0;
Each time you increment the city count for a state,
check if that count is > $MostCities
Update $Mostcities
$StateMost
stage 6:
As you add data to the hash,
keep track of which state has the fewest cities.
see example in class 7
$Mincities = -1;
Each time you find a new city,
you are going to update the count for that state.
if $Mincities == -1, set $MinCities to this count.
If any state has fewer cities than $mincities,
update $mincities
$Statemin