On Tue, 24 Apr 2007 13:33:32 +0200, Andrej Kastrin wrote:

> Question about the sum function; the file structure is as follows:
> 
> A|100
> A|200
> A|150
> B|20
> B|90
> C|10
> C|30
> C|300
> 
> The result I want to obtain is to sum values in the second column 
> (columnB) for each particular letter in the first column (ColumnA); e.g.:
> 
> A|450
> B|100
> C|330
> 
> I don't want to use hash structure because the input file is very large. 
> Is there any simple way to do that step-by-step: to sum up values in 
> columnB until the letter in columnA changes and print the result...

If the first column is only a letter how many possible values could there
be?  How could a hash get too big?  You wouldn't even need a script, you
could do it in one line:

$ cat input
A|100
A|200
A|150
B|20
B|90
C|10
C|30
C|300

$ perl -F'\|' -lane '$h{$F[0]} += $F[1]; END{print "$_: $h{$_}" \
                     for sort keys %h}' input 
A: 450
B: 110
C: 340

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to