Home > Archive > PERL Beginners > May 2006 > Sum problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Andrej Kastrin 2006-05-29, 7:59 am |
| Dear Perl users,
I have 2 columns, tab separated file which looks like
A 4
B 3
C 1
A 3
B 3
First column refers to name and second to value. I have to calculate
total score on each 'name'; in my case:
A 7
B 6
C 1
Here is the code; but it doesn't work.
#####################################
while (<> ) {
chomp;
($name,$value)=split(/\t/);
$values{$name}+=$value;
}
foreach $w (sort by_number keys %values) {
print "$w\t$value{$w}\n"
}
#####################################
Thanks in advance for any suggestions or notes.
Andrej
| |
| Mohammed Shameer 2006-05-29, 7:59 am |
| Hi,
=20
You should always have
use strict;
use warnings;
in your perl code.
u misspelled the hash %values in the for loop
On Monday 29 May 2006 16:47, Andrej Kastrin wrote:
> foreach $w (sort by_number keys %values) {
> =A0 =A0 print "$w\t$value{$w}\n"
> }
Shameer
=2D-=20
| |
| Chandru 2006-05-29, 7:59 am |
| Hey
problem with your for loop
#!/usr/bin/perl -w
while (<> ) {
chomp;
($name,$value)=split(/\t/);
$ha{$name}+=$value;
}
foreach $i(sort keys %ha)
{
print$i." ".$ha{$i}."\n";
}
- Chandru
Andrej Kastrin wrote:
> Dear Perl users,
>
> I have 2 columns, tab separated file which looks like
>
> A 4
> B 3
> C 1
> A 3
> B 3
>
> First column refers to name and second to value. I have to calculate
> total score on each 'name'; in my case:
> A 7
> B 6
> C 1
>
> Here is the code; but it doesn't work.
> #####################################
> while (<> ) {
> chomp;
> ($name,$value)=split(/\t/);
> $values{$name}+=$value;
> }
>
> foreach $w (sort by_number keys %values) {
> print "$w\t$value{$w}\n"
> }
> #####################################
>
> Thanks in advance for any suggestions or notes.
>
> Andrej
>
|
|
|
|
|