Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I need to write a perl script that will parse census data (http://pastebin.c

ID: 3546999 • Letter: H

Question

Hi,
I need to write a perl script that will parse census data (http://pastebin.com/hNzke4V8).

The script needs to parse the data, and for each county, print the county name, the population per square mile of land (population/land area), and the percentage of the county that is water (water area / (land area + water area)).

At the end, the script needs to print the county name and the value for the following criteria.
Highest population density
Lowest population density
Highest percentage of water
Lowest percentage of water

Below is an example of what the output should look like:
County Population/sq mile % Water
Adams County 307.2 88.1%
Asotin County 111.8 12.6%
[... etc ...]

Highest population density: Adams County, 9999 people/square mile
Lowest population density: Pierce County, 3 people/square mile
Highest percentage of water: Whitman County, 90.2% water
Lowest percentage of water: Skagit County, 3.6% water


Any help you could provide would be very much appreciated.

Explanation / Answer


#!/usr/bin/perl

use strict;

my $filename = 'file.txt';

my $hpd;

my $lpd=0;

my $hpw;

my $lpw=0;

my $hpdc;

my $lpdc;

my $hpwc;

my $lpwc;

my $i=0;

open(my $fh, $filename)

or die "Could not open file '$filename' $!";

print "County Population/sq mile % Water ";

while (my $row = <$fh>) {

chomp $row;

my @data=split(/,/,$row);

my $pop = $data[1];

my $land = $data[2];

my $water = $data[3];

if($land > 0){

my $pd = $pop/$land;

my $pw = $water/($land+$water);

if($i==0){

$lpd=$pd;

$lpw=$pw;

$lpdc=$data[0];

$lpwc=$data[0];

$i=1;

}

if($pd>$hpd){

$hpd=$pd;

$hpdc=$data[0];

}

elsif($pd<$lpd){

print "true";

$lpd=$pd;

$lpdc=$data[0];

}

if($pw>$hpw){

$hpw=$pw;

$hpwc=$data[0];

}

elsif($pw<$lpw){

$lpw=$pw;

$lpwc=$data[0];

}

print "$data[0] $pd $pw ";

}

}

print "Highest population density: $hpdc , $hpd people/square mile ";

print "Lowest population density: $lpdc , $lpd people/square mile ";

print "Highest percentage of water: $hpwc , $hpw % water ";

print "Lowest percentage of water: $lpwc , $lpw % water ";

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote