psychoto has asked for the wisdom of the Perl Monks concerning the following question:
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::
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:
Thanks in advance Great and Mighty Monks!!!
The basic code for the program is as follows::
My input file 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"; }
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!!!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: I/O problems...what am I missing?
by sauoq (Abbot) on Sep 16, 2002 at 05:58 UTC | |
Re: I/O problems...what am I missing?
by dws (Chancellor) on Sep 16, 2002 at 06:12 UTC | |
by clintp (Curate) on Sep 16, 2002 at 13:37 UTC | |
by dws (Chancellor) on Sep 16, 2002 at 18:11 UTC | |
Re: I/O problems...what am I missing?
by kabel (Chaplain) on Sep 16, 2002 at 06:04 UTC | |
Re: I/O problems...what am I missing?
by csotzing (Sexton) on Sep 16, 2002 at 10:53 UTC | |
by slife (Scribe) on Sep 16, 2002 at 12:57 UTC | |
by psychoto (Novice) on Sep 17, 2002 at 16:46 UTC |
Back to
Seekers of Perl Wisdom