in reply to Re^2: how do I manipulate data in csv file
in thread how do I manipulate data in csv file

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

Replies are listed 'Best First'.
Re^4: how do I manipulate data in csv file
by Anonymous Monk on Jul 26, 2011 at 06:10 UTC

    hi, sorry ya, i am newbie in perl

    can i know what if i have a csv file like below?

    week power test Test_end_date_time 201120 5 1 05-10-2011_16:47 201120 699 17 05-10-2011_16:48 201120 454 1 05-10-2011_16:49 201121 5687 26 12-10-2011_15:47 201121 211 1 12-10-2011_15:48 201121 1231 36 12-10-2011_15:49 201121 171 1 12-10-2011_15:50 201121 677 57 12-10-2011_15:51 201121 178 1 12-10-2011_15:52 if test = 1 mean pass, other than 1 is fail I wish the output in another csv is like below, is it correct to use @ +field?? week volume yield 201120 3 66.7% 201121 6 50.0%
      Maybe if you ask one more time, you'll get a different answer?