in reply to Re: array in for loop
in thread array in for loop

perhaps if I send in the entire code and the data file.

open scores, "a:/scores.txt" || die "what did you idiots do with the s +cores file."; while(<scores>){ chomp; my (@names, @score) = split /:/; $dev=1; for($ctr=0; $ctr<@score; $ctr++){ print "@score\n"; $a = $a + @score; $dev++; print "$dev"; } $b = $a / $dev; print "the average of the scores is $b "; }

zon moy:100:jack douglass:99:abe douglass:98:thomas severson:97:bobby beavers:96:carla steinke:95:bona hayes:94:mike fedie:93:norma sanders:92:jamie maddox:91:scott summers:90:alex summers:89:kitty pryde:88:doug ramsey:87:clarence thomas:86:loki moy:85

Will this info help

Replies are listed 'Best First'.
Re: Re: Re: array in for loop
by jasonk (Parson) on Feb 27, 2003 at 18:24 UTC

    @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.

      Cant use hashes in the program.