117 Homework Assignment #10 (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: proj10.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.

- Your program should list every line from the file
   (except the first line which is the header line)
    but only display the following from each line:
      - the city name without the status and without the leading 2
          punctuation chars <_
      - the 2003 population
      - the percentage of that population compared to the whole state
          Calculate the whole state population by
          adding all the city populations in that state.
          You will need 2 phases:
              1. add up the pop of each state
              2. display each city as described
          There are 2 ways to approach this program:
              1. read the file twice
              2. in phase 1, read the file and save the data
                 in an array or hash
                 in phase 2, process from the array or hash.

- Use the format statement as found in chap 12
   so that the city name starts in col 3
   the 2003 pop ends in col 50
   the percentage has a decimal point in col 60
   and 2 digits after the decimal point.
     Hint see page 369 to line up the decimal points.

     New Hint:
     add a comment that shows column numbers before your format
     statement:
#        1         2         3         4         5         6         7
#234567890123456789012345678901234567890123456789012345678901234567890123456789
      @||||||               @############.#######   

- hint: use code like this to remove the status from the city name:
   $citystat =~ s/(.+)_[^_]+$/$1/;  
     # above to remove status by erase from last underscore

- 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.

=======================================

Hint: write the program in stages

   stage 1: create a hash where each key is a state
      and the value of each state key 
       is the population for that state.
      Detail:
         for each city,
          check if the state is defined in the state hash.
           If yes, add that city population to its state value.
           If not, add that state to the hash with the city
            population as its initial population.

   stage 2:
      close the usa file and open it again
      add a 2nd loop
       where you read the usa file exactly as you did in stage 1.
        output the city name, its 2003 population,
         and what percent that population is compared to the whole state.
          Just put one space between city name, 2003 pop, and the percent.

   stage 3:
      now make the output of the 2nd loop
       conform to the program specification.
        use a format statement
         to align the output to the correct column positions.