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

Reply via email to