Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading text file loop

Status
Not open for further replies.

JtowelPT

Programmer
Jun 28, 2003
1
US
Unfortunately Perl is not my native language and I've just begun to experiment with it, so I apologize if my question is novice.

I've properly written user submitted scores to a text file called "scores.txt" with "::-::" seperating the scores. The text file looks something like:
"8::-::10::-::10::-::9"

All scores are between 1-10 and there is no limit to how many user submitted scores there are.

I am attempting to create a loop which will both add all of the numbers together and count how many there are (in order to calculate the average).

Thanks in advance for any help.
 
There may be more efficient ways of doing it, but one way is:

my (@list, $sum, $count, $avg);
while (<>) {
@list = split /::-::/;
$count = scalar @list;
$sum = 0;
foreach (@list) {
$sum += $_;
}
print &quot;$count entries, average = &quot;, $sum / $count, &quot;\n&quot;;
}

-Bill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top