Something as crude as use Data::Dumper; open( FILE, '< file' ) || die $!; my ( $line, $ela, $elb, $pre, %count, $i, $tot); $tot = 0; while ( $line = <FILE> ) { $i++; ( $ela, $elb ) = split ( '\|', $line );
# # remove any white space # $elb=~s/\s+$//g; $elb=~s/^\s+//g; # # dont do the first element as will always # be different # if ( ( $ela ne $pre ) && $i != 1 ) { $count{$pre} = $tot; $tot = 0; $tot += $elb; } else { $tot += $elb; } $pre=$ela; } # # get the final row total # $count{$pre} = $tot; print Dumper(\%count); Will probably do the job -----Original Message----- From: Andrej Kastrin [mailto:[EMAIL PROTECTED] Sent: 24 April 2007 12:34 To: beginners@perl.org Subject: How to sum up values Dear all, 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... Thanks in advance for any suggestion, Andrej #!/usr/bin/perl use strict; use warnings; open FH1, "< test.txt" or die "Can't open file : $!"; while (<FH1>) { chomp; ($columnA,$columnB)=split /\|/; ... ??? } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ This e-mail is from the PA Group. For more information, see www.thepagroup.com. This e-mail may contain confidential information. Only the addressee is permitted to read, copy, distribute or otherwise use this email or any attachments. If you have received it in error, please contact the sender immediately. Any opinion expressed in this e-mail is personal to the sender and may not reflect the opinion of the PA Group. Any e-mail reply to this address may be subject to interception or monitoring for operational reasons or for lawful business practices. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/