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

1. The name, telephone number, and quarterly sales totals, separated by ‘:’, for

ID: 3770456 • Letter: 1

Question

1. The name, telephone number, and quarterly sales totals, separated by ‘:’, for a list of salespersons for a certain company.

Steve Blenheim:238-923-7366:20300:21320:22390:21500
Betty Boop:245-836-8357:14500:14070:14360:15370
Igor Chevsky:385-375-8395:23400:24340:25320:24820
Norma Corder:397-857-2735:24570:25070:24320:28700
Jennifer Cowan:548-834-2348:58900:53010:55670:52330
Jon DeLoach:408-253-3122:85100:95320:92470:94320
Karen Evich:284-758-2857:85100:97870:101790:101790
Fred Fardbarkle:674-843-1385:78090:84340:86030:92060
Lori Gortz:327-832-5728:35200:36260:32640:31670
Paco Gutierrez:835-365-1284:12350:11860:13290:13690
Ephram Hardy:293-259-5395:56700:60670:69780:68390
James Ikeda:834-938-8376:45000:44100:47630:50020
Barbara Kertz:385-573-8326:26850:31690:32650:33310
Lesley Kirstin:408-456-1234:52600:49450:47480:49380
William Kopf:846-836-2837:43500:44370:47480:46060
Sir Lancelot:837-835-8257:24500:24500:24010:25220
Jesse Neal:408-233-8971:25000:26750:31570:30630
Zippy Pinhead:834-823-8319:89500:86820:81620:84890
Arthur Putie:923-835-8745:12600:12980:13240:13510
Popeye Sailor:156-454-3322:22350:21910:21910:19720
Jose Santiago:385-898-8357:95600:100380:107410:120300
Tommy Savage:408-724-0140:34200:34890:33850:38930
Yukio Takeshida:387-827-1095:57000:59280:61060:65950
Vinh Tranh:438-910-7449:68900:66840:65510:67480

2. Write a perl script that does all of the following:

a) Prints the heading, “Sales Figures”

b) Print the names of all salespersons in the form LastName, FirstName, their total sales for all four quarters, and their average quarterly sales.

c) Prints a summary at the end of the list that gives the number of salespersons, the average sales of the salespersons, the average sales for each quarter, and the name and total sales of the salesperson with the highest total sales.

Explanation / Answer

$infile = 'test.txt';

open(FILE, $infile) or die "not able to open $infile, program halting.";
print " name phone number qtr1 qtr2 qtr3 qtr4 avg ";
my $qtr1 =0;
my $qtr2 =0;
my $qtr3 =0;
my $qtr4 =0;
my $avg_all =0;
my $max_name;
my $max_sales =0;
my $n = 0;

while(<FILE>)
{
# get rid of the pesky newline character
chomp;

# read the fields in the current line into an array
@fields = split(':', $_);
@name = split(' ',$fields[0]);
$avg = ($fields[2]+$fields[3]+$fields[4]+$fields[5])/4;
# print the first field
print "$name[1], $name[0] $fields[1] $fields[2] $fields[3] $fields[4] $fields[5] $avg ";
$qtr1= $qtr1 + $fields[2];
$qtr2= $qtr2 + $fields[3];
$qtr3= $qtr3 + $fields[4];
$qtr4= $qtr4 + $fields[5];
$avg_all = $avg_all + $avg;
$n = $n + 1;
if ($avg > $max_sales)
{
$max_sales = $avg;
$max_name = "$name[1], $name[0]";
}
}

print " SUMMARY ";
$qtr1 = $qtr1 / $n;
$qtr2 = $qtr2 / $n;
$qtr3 = $qtr3 / $n;
$qtr4 = $qtr4 / $n;
$avg_all = $avg_all/ $n;
$max_sales = $max_sales * 4;
print "average for quarter 1 = $qtr1 ";
print "average for quarter 2 = $qtr2 ";
print "average for quarter 3 = $qtr3 ";
print "average for quarter 4 = $qtr4 ";
print "total average = $avg_all ";
print "Max sales are by $max_name and total sales by him = $max_sales";
close FILE;

output: