On Apr 7, 2005 6:33 AM, Bill Evans wrote:
> 
> In this scenario, I have a text file, the first line of which contains a bank 
> account number and 
> opening balance. The second line contains records of checks drawn on the 
> account. The last 
> line contains a zero amount. Im trying to code a script that will read and 
> print to a file the 
> account number, opening balance and check records. In addition I wish that 
> the current balance 
> is displayed along with the check records (a running balance).
> 

Hi,
The first problem in your code is that you don't use "use strict;" and
"use warnings;" in your code. This causes you not to catch many other
errors, such as having the line:
   $line = readline(INPUT);
but INPUT is not defined anywhere, or:
   $totalafterfirst = $noopbal[3] - $cheques[1];
but @cheques is not defined (you used @checks in other places).

Other then that, you are almost okay, but the code you used to
calculate the running balance is too complex. Think of this
psuedo-code:

foreach check
    balance = current balance minus check amount
    print balance
end for loop

This can be translated almost as is to Perl code. Here is a version
that works, and that also includes some "prettying up" of your code,
such as combining the arguments to print into one string:
#################### begin code
use strict;
use warnings;
my $infile = "bank.txt";
open(TEXT, $infile) || die("Cannot find file: $! !\n");
open(OUTPUT, ">report.txt") || die ("Cannot open output file: $! !\n");
my $line = <TEXT>;
my @noopbal = split(/!/, $line);
print "The Account Number is $noopbal[1]\n";
my $balance = $noopbal[3];
print "The opening balance of this account was $balance\n";
print "Printing report to file...\n";
print(OUTPUT "Statement of account report\n\n");
print(OUTPUT "Acc Number: $noopbal[1]\n");
print(OUTPUT "Opening Balance: $noopbal[3]\n\n");
$line = <TEXT>;
my @checks = split(/\@/, $line);
# get rid of empty last and first elements:
shift @checks; pop @checks;
print(OUTPUT "Cheque Debits  @checks\n");
print(OUTPUT "Current Balance: ");
for (@checks) 
{
   $balance -= $_;
   print(OUTPUT "$balance ");
}
print(OUTPUT "\n");
#################### end code

-- 
Offer Kaye

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


Reply via email to