in reply to Re: Re: array in for loop
in thread array in for loop
@score is empty, because my(@names,@score) = list will put all the list elements in @names. If you want average scores try this instead:
open SCORES, "a:/scores.txt" or die "no scores!"; while(<SCORES>) { chomp; # turn the data into a hash, with names as keys # and scores as values my %scores = split(':',$input); # add up all the values my $total = 0; map { $total += $_; } values %scores; # divide the total by the number of names in the hash # to get the average. my $average = $total / (keys %scores); print "the average of the scores is $average\n"; } close(SCORES);
This assumes you only want an average of the scores on each line, and that the same name never appears twice on one line of your data file.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: array in for loop
by zonmoy (Initiate) on Feb 27, 2003 at 19:03 UTC |