Gossamer Forum
Home : General : Perl Programming :

ADD the Records?

Quote Reply
ADD the Records?
Okay lets Open the database
------------------------
open (ORGDB,"<$database");
@ODB=<ORGDB>;
close (ORGDB);

##Split and read the contents of database
foreach $rec (@ODB){
chomp($rec);
($employee_name,$pay)=split(/\|/,$rec);

print "$employee_name" - $pay\n";

-------------------------
All the above code is working fine. The problem is I want to
calculate all the $pay records in the End.

e.g. TOTAL PAY given to employees.

How I can calculate/add all the $pay records in the database and
show them at the last line??

Any help?

Thanks and regards to all,

Zeshan.

Quote Reply
Re: [zeshan] ADD the Records? In reply to
I'll bet you $100,000 that your code exactly as you have it does not run Wink

I'd suggest the following, less memory intensive solution (including error checking Wink)...

Code:
my $total_pay = 0;
my $database = "/foo/bar';
open ORGDB, $database or die "Can't open $database: $!";
while (<ORGDB>) {
chomp;
my ($employee_name,$pay) = split /\|/, $rec;
print "$employee_name - $pay\n";
}
close ORGDB;

print $total_pay;

Last edited by:

Paul: Mar 12, 2003, 1:31 PM
Quote Reply
Re: [Paul] ADD the Records? In reply to
I bet yours doesn't run properly either. =)

I think you want a $total_pay += $pay somewhere in there.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] ADD the Records? In reply to
Depends if you had:

use strict;

in there Wink
Quote Reply
Re: [Paul] ADD the Records? In reply to
How can a WHILE loop be less memory intensive?

- wil
Quote Reply
Re: [Wil] ADD the Records? In reply to
Because you are not loading the entire database into memory with:

@ODB=<ORGDB>;

Cheers,

Alex
--
Gossamer Threads Inc.