First, use Text::CSV_XS instead of Text::ParseWords - it reads more like the spec that way (having your code match your spec is always good in my books). Second, I don't see any commas in your original data, so there is an obvious mismatch.
Next, you are hardcoding your week number in your code. Don't do that. Accumulate it in a hash. I would even go so far as use a hash of hashes with separate fields for the sum and count.
Also, learn to use proper indentation. Your code is quite simply a mess. It's very difficult to follow nesting, and would get impossible to follow fairly quickly.
I also see a while inside a while, both reading from the same file. That's nonsensical. Instead, just read and discard a single line: scalar <$fh> should do it.
That should give you enough to go on.my %data; my $csv = Text::CSV_XS->new(); scalar <$fh>; while (my $row = $csv->getline($fh)) { $data{$row->[0]}{sum} += $row->[1]; $data{$row->[0]}{count}++; } for my $weeknum (sort keys %data) { printf "%s %d %.1f\n", $weeknum, $data{$weeknum}{sum} / $data{$weeknum}{count} +, $data{$weeknum}{count}; }
Update: s/comments/comma/ in the first paragraph
In reply to Re^3: how do I manipulate data in csv file
by Tanktalus
in thread how do I manipulate data in csv file
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |