你可以帮我解决下面的逻辑吗?我需要在一行中打印特定“ID”的Active和status。当我执行以下逻辑时,它会为每个ID打印两次。
我得到输出(这是错误的)
Totals,,, 31,0,53
Totals,,, 0,31,53
Totals,,, 60,0,61
ID value Days
53 ACTIVE 31
53 SUSPEND 31
61 ACTIVE 60
我需要看到像
这样的输出Totals,,, 31,31,53
Totals,,, 60,,61
代码:
while ( my ( $ID, $value, $DAYS ) = $Query->fetchrow_array() ) {
my $activeID = $ID;
my $activedays = "";
my $suspenddays = "";
if ($activeID) {
if ( $value eq "ACTIVE" ) {
$active = $DAYS;
}
if ( $value eq "SUSPEND" ) {
$suspend = $DAYS;
}
}
printf $OUTPUT "Totals,,,%d,%d,%d\n", $active, $suspend, $activeID;
}
答案 0 :(得分:0)
my %COUNT = ();
while ( my ( $ID, $value, $DAYS ) = $Query->fetchrow_array() ) {
if ($ID) {
# assumes active lines come before suspend lines in the table for a particular ID
push (@$COUNT{$ID},$days);
}
}
foreach my $key (keys %COUNT) {
my ($active, $suspend);
$active = $$COUNT{$key}[0] if (defined $$COUNT{$key}[0]);
$suspend = $$COUNT{$key}[1] if (defined $$COUNT{$key}[1]);
print "Totals,,,$active,$suspend,$key\n";
}