I've written a very basic program that reads one long line of data from an external file and the data is supposed to be sorted and then displayed. Very basic stuff here. I wrote the code without using any sort opperator, because I wanted a change. My output isn't quite what I'm expecting and for the life of me I cannot locate the problem....Any ideas?
The basic code for the program is as follows::
open INPUT, "a:scores.txt" || die "Cannot locate input file: $!";
while (<INPUT>){
$ctr = 0;
chomp;
($class, @array) = split /:/;
foreach(@array){
($names[$ctr],$score[$ctr]) = split /,\s+/;
$ctr++;
}
}
$control = @score;
for ($ctr=0; $ctr<$control; $ctr++){
for($sub=$control-1; $sub>$ctr; $sub--){
if ($score[$sub]<$score[$sub-1]){
$temp = $score[$sub];
$score[$sub] = $score[$sub-1];
$score[$sub-1] = $temp;
$temp2 = $names[$sub];
$names[$sub] = $names[$sub-1];
$names[$sub-1] = $tem;
}
}
}
foreach(@score){
$total += $score[$ct];
$ct++;
}
$avg = $total/$ct;
print "\n\n\tThe class $class has $control students:\n\n";
print "\tThe lowest test score was: $score[1]\n";
print "\tThe highest test score was: $score[$control-1]\n";
print "\tThe average test score was:";
printf ("%.2f %s", $avg,"\n\n");
for ($ctr=0; $ctr<=$control; $ctr++){
print "\t$names[$ctr] scored $scores[$ctr]\n";
}
My input file is as follows:
Perl Programming:Joe Smith, 87:Jane Thompson, 93:Ed Jones, 75:Nancy Edwards, 84:James Allen, 64:Peggy Johnson, 68:Bill Swanson, 98:Thomas Jefferson, 76:Abraham Lincoln, 71:John Adams, 84:Theodore Roosevelt, 66:John F. Kennedy, 80:Jimmy Carter, 75:Ronald Reagan, 97:Grover Cleveland, 95:Dwight D. Eisenhower, 96:George Bush, 94:
The desired output I am hoping for is:
Line 1 -- A header displayed with class title and # of students.
Line 2 -- Lowest score on test
Line 3 -- Highest score
Line 4 -- Average
Line 5 -- blank
Line 6+ (Name of student) scored (score) sorted from low score to high score.
Currently my output is very scattered. Only 3 students are displayed, and no scores are displayed other than High/low/average. I believe my pseudo-sort is funtioning...but I'm unsure.
I've stared aimlessly at this code for 2 hours now and thought maybe I should post if for all the "gurus" to glance at it and fix my problem in 12 seconds.
Thanks in advance Great and Mighty Monks!!!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.