Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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!!!

In reply to I/O problems...what am I missing? by psychoto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-24 05:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found