我有一个 csv 文件,transcation.csv
它看起来像这样:
TransactionID,Date,AccNum,Type,Amt
657520,02-07-1999,016901581432,Debit,16000
657524,02-07-1999,016901581432,Debit,13000
657538,09-07-1999,016901581432,Credit,11000
657548,18-07-1999,016901581432,Credit,15500
657519,02-07-1999,016901581433,Debit,12000
657523,02-07-1999,016901581433,Credit,11000
657529,03-07-1999,016901581433,Debit,15000
657539,10-07-1999,016901581433,Credit,10000
657541,11-07-1999,016901581434,Debit,12000
657525,03-07-1999,016901581434,Debit,15000
657533,05-07-1999,016901581434,Credit,12500
我应该从中读取数据transaction.csv
并将其存储在哈希中。然后将哈希和帐号列表传递给函数,然后使函数使用哈希中的数据计算列出的账户的总借方金额和贷方金额。
这是我的代码:
#!/usr/bin/perl
my %banks;
&arrhas_banks(\%banks);
my @accnum = qw(016901581432 016901581434);
hasarr_banks(\%banks,\@accnum);
sub arrhas_banks
{
my $banks = shift;
open(FH,"<transaction.csv") or die "Could not open 'transaction.csv' $!\n";
while (my $data=<FH>)
{
chomp($data);
my @records = split ",", $data;
$banks->{$records[0]} =
{ 'AccNum' => $records[2], 'Type' => $records[3], 'Amt' => $records[4] };
}
close(FH);
}
sub hasarr_banks {
my ($banks, $accnum) = @_;
foreach my $id (keys %{$banks}) {
my $answer;
foreach my $num (@{$accnum}) {
if($banks->{$id}{'AccNum'} == $num) {
$answer = 1;
}
}
if($answer) {
my $type = $banks->{$id}{'Type'};
$total{$accnum}{$type} += $banks->{$id}{'Amt'};
}
}
foreach my $no (@{$accnum}} {
print "Debit amount of $no is $total{$accnum}{'Debit'}";
print "Credit amount of $no is $tot{$accnum}{'Credit'}";
}
}
我应该产生这样的输出:
Debit amount of 016901581432 is 29000
Credit amount of 016901581432 is 26500
Debit amount of 016901581434 is 27000
Credit amount of 016901581434 is 12500
但我得到这样的输出:
Debit amount of 016901581432 is 56000
Credit amount of 016901581432 is 39000
Debit amount of 016901581434 is 56000
Credit amount of 016901581434 is 39000
我究竟做错了什么?
#!/usr/bin/perl
my %banks;
&arrhas_banks(\%banks);
my @accnum = qw(016901581432 016901581434);
hasarr_banks(\%banks,\@accnum);
sub arrhas_banks
{
my $banks = shift;
open(FH,"<transaction.csv") or die "Could not open 'transaction.csv' $!\n";
while (my $data=<FH>)
{
chomp($data);
my @records = split ",", $data;
$banks->{$records[0]} =
{ 'AccNum' => $records[2], 'Type' => $records[3], 'Amt' => $records[4] };
}
close(FH);
}
sub hasarr_banks {
my ($banks, $accnum) = @_;
foreach my $id (keys %{$banks}) {
my $answer;
foreach my $num (@{$accnum}) {
if($banks->{$id}{'AccNum'} == $num) {
$answer = 1;
}
}
if($answer) {
my $type = $banks->{$id}{'Type'};
$total{ $banks->{$id}{'AccNum'} }->{$type} += $banks->{$id}{'Amt'};
}
}
foreach my $no (@{$accnum}) {
print "Debit amount of $no is $total{$accnum}{'Debit'}";
print "Credit amount of $no is $total{$accnum}{'Credit'}";
}
}
还没有输出。还是出错了?