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.

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}; }
That should give you enough to go on.

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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.