The code below is what I got so far, but I'm still getting the "|0.0" for the first row returned. I don't know which part of the code is generating this, could someone help explain this to me? Thanks for all your help.
new_ratings.txt: base_no|AvgRating |0.0 10000|4.7 10007|4.5 10008|2.5 #!/usr/bin/perl ####################################################################### # This script reads through reviewsRating.txt and calculates an average # # rating for each base_no # ####################################################################### use strict; use warnings; my %numsum; my %numcnt; my $output= 'new_ratings.txt'; my $file ='reviewsRating.txt'; #Open output file to print header row open(OUT,">$output") or die "Could not open $output: $!"; print OUT "base_no|AvgRating\n"; close(OUT); open my $fh, '<', $file or die $!; open OUT, '>>', $output or die "Could not open '$output' $!"; while(<$fh>){ next if $. == 1; # exclude header chomp; my ($num, $rate) = split/\|/; $numsum{$num} += $rate; ++$numcnt{$num}; } for (sort keys %numsum){ my $numavg = sprintf "%.1f",$numsum{$_} / $numcnt{$_}; print OUT "$_\|$numavg\n"; } close($fh); close(OUT); ________________________________ From: michael wang [mailto:[EMAIL PROTECTED] Sent: Friday, December 07, 2007 2:29 PM To: Mike Tran Cc: beginners@perl.org Subject: Re: Calcualting Avg. Hi On 12/7/07, Mike Tran <[EMAIL PROTECTED]> wrote: Hi All, I'm not very familiar with Perl yet, so could someone help me with this please? I have a file which has two columns one called Basenumber the other Rating (Rating.txt), how do I loop through and get an average for the Rating column for each distinct baseNumber? I want to write the new result out into a new file called AvgRatings.txt. Thanks in advance for any help on this. Rating.txt: Basenumber|Rating 10000|5 10000|4 10000|5 10007|4 10007|5 10007|4 10007|5 10008|4 10008|3 10008|2 10008|1 The new file (AvgRatings.txt) should looks like this: Basenumber|AvgRating 10000|4.67 10007|4.5 10008|2.5 Best regards, Mike Tran e: [EMAIL PROTECTED] t: (307) 772-8956 Learn perl hash something like: use strict; use warnings; my %numsum; my %numcnt; while(<DATA>){ chomp; my ($num, $rate) = split/\|/; $numsum{$num} += $rate; ++$numcnt{$num}; } print "Basenumber|AvgRating\n"; for (sort keys %numsum){ my $numavg = $numsum{$_} / $numcnt{$_}; print "$_\|$numavg\n"; } __DATA__ put your data here