C
chadmay
I have a flat file that had a mix of fixed length and variable length
records. I am parsing through each record and determining if each
field is accurate compared to the document. Dealing with the fixed
length records was easy, but I'm having difficulting with the variable
length records. Here is a snippet of my code and record 3
(ACCT_CLUS_NM) is a variable length record. Since the record in my
test file isn't 45 chars, my output file is incorrect. Since I know
that the largest that record 3 will be is 45 chars, how do I code for
it??
#!/usr/bin/perl
$loop=57;
$ifname=$ARGV[0];
$ofname=$ARGV[1];
if("${ifname}" eq "" | "${ofname}" eq "")
{
print "\nUsage: $0 input_file output_file\n";
exit;
}
if(!-f "$ifname" | !-r "$ifname")
{
print "$ifname doesn't exist or you do not have permission to read
it.\n";
exit 1;
}
if(-f "$ofname")
{
print "$ofname already exists.\n";
exit 1;
}
open(BATCHF, "$ifname") or die "Open failed: $!\n";
open(FBATCHF, ">> $ofname") or die "Open failed: $!\n";
while (<BATCHF>)
{
$RECIN=$_;
$fpos=0;
for ($cnt=1;$cnt<=$loop;$cnt++)
{
if($cnt==1){$reclength=4;$field="ACCOUNT_TYPE"};
if($cnt==2){$reclength=1;$field="TAB"};
if($cnt==3){$reclength=45;$field="ACCT_CLUS_NM"};
if($cnt==4){$reclength=1;$field="TAB"};
if($cnt==5){$reclength=9;$field="ACCT_NBR"};
if($cnt==6){$reclength=1;$field="TAB"};
if($cnt==7){$reclength=3;$field="CORP_ENT_CD"};
if($cnt==8){$reclength=1;$field="TAB"};
if($cnt==9){$reclength=9;$field="CUSTOMER_NO"};
if($cnt==10){$reclength=1;$field="TAB"};
if($cnt==11){$reclength=20;$field="D_BEN_AGMT_DESC"};
if($cnt==12){$reclength=1;$field="TAB"};
if($cnt==13){$reclength=10;$field="D_BEN_AGMT_EFF_DT"};
if($cnt==14){$reclength=1;$field="TAB"};
if($cnt==15){$reclength=8;$field="D_FUND_TYP_CD"};
if($cnt==16){$reclength=1;$field="TAB"};
if($cnt==17){$reclength=6;$field="D_PROD_TYP_CD"};
if($cnt==18){$reclength=1;$field="TAB"};
if($cnt==19){$reclength=8;$field="FOLDED_FUNDING_TYPE"};
if($cnt==20){$reclength=1;$field="TAB"};
if($cnt==21){$reclength=40;$field="GROUP_NAME"};
if($cnt==22){$reclength=1;$field="TAB"};
if($cnt==23){$reclength=9;$field="GROUP_NO"};
if($cnt==24){$reclength=1;$field="TAB"};
if($cnt==25){$reclength=2;$field="GRP_CAN_CD"};
if($cnt==26){$reclength=1;$field="TAB"};
if($cnt==27){$reclength=10;$field="GRP_CAN_DT"};
if($cnt==28){$reclength=1;$field="TAB"};
if($cnt==29){$reclength=10;$field="GS_ORIGL_EFF_DT"};
if($cnt==30){$reclength=1;$field="TAB"};
if($cnt==31){$reclength=20;$field="H_BEN_AGMT_DESC"};
if($cnt==32){$reclength=1;$field="TAB"};
if($cnt==33){$reclength=10;$field="H_BEN_AGMT_EFF_DT"};
if($cnt==34){$reclength=1;$field="TAB"};
if($cnt==35){$reclength=8;$field="H_FUND_TYP_CD"};
if($cnt==36){$reclength=1;$field="TAB"};
if($cnt==37){$reclength=6;$field="H_PROD_TYP_CD"};
if($cnt==38){$reclength=1;$field="TAB"};
if($cnt==39){$reclength=10;$field="LOAD_DT"};
if($cnt==40){$reclength=1;$field="TAB"};
if($cnt==41){$reclength=10;$field="LST_UPDT_DT"};
if($cnt==42){$reclength=1;$field="TAB"};
if($cnt==43){$reclength=4;$field="MKT_SEG_CD"};
if($cnt==44){$reclength=1;$field="TAB"};
if($cnt==45){$reclength=45;$field="NAME"};
if($cnt==46){$reclength=1;$field="TAB"};
if($cnt==47){$reclength=10;$field="ROW_EFFECTIVE_DT"};
if($cnt==48){$reclength=1;$field="TAB"};
if($cnt==49){$reclength=10;$field="ROW_END_DT"};
if($cnt==50){$reclength=1;$field="TAB"};
if($cnt==51){$reclength=5;$field="SECT_NBR"};
if($cnt==52){$reclength=1;$field="TAB"};
if($cnt==53){$reclength=40;$field="SECT_NM"};
if($cnt==54){$reclength=1;$field="TAB"};
if($cnt==55){$reclength=4;$field="SIC_CD"};
if($cnt==56){$reclength=1;$field="TAB"};
if($cnt==57){$reclength=10;$field="TIER_TYPE"};
chomp($RECIN);
$RECOUT=substr($RECIN,$fpos,$reclength);
$fpos=$reclength + $fpos;
print FBATCHF "$field $reclength |$RECOUT|\n";
}
print FBATCHF "------------------------------------------------\n";
}
close BATCHF;
close FBATCHF;
records. I am parsing through each record and determining if each
field is accurate compared to the document. Dealing with the fixed
length records was easy, but I'm having difficulting with the variable
length records. Here is a snippet of my code and record 3
(ACCT_CLUS_NM) is a variable length record. Since the record in my
test file isn't 45 chars, my output file is incorrect. Since I know
that the largest that record 3 will be is 45 chars, how do I code for
it??
#!/usr/bin/perl
$loop=57;
$ifname=$ARGV[0];
$ofname=$ARGV[1];
if("${ifname}" eq "" | "${ofname}" eq "")
{
print "\nUsage: $0 input_file output_file\n";
exit;
}
if(!-f "$ifname" | !-r "$ifname")
{
print "$ifname doesn't exist or you do not have permission to read
it.\n";
exit 1;
}
if(-f "$ofname")
{
print "$ofname already exists.\n";
exit 1;
}
open(BATCHF, "$ifname") or die "Open failed: $!\n";
open(FBATCHF, ">> $ofname") or die "Open failed: $!\n";
while (<BATCHF>)
{
$RECIN=$_;
$fpos=0;
for ($cnt=1;$cnt<=$loop;$cnt++)
{
if($cnt==1){$reclength=4;$field="ACCOUNT_TYPE"};
if($cnt==2){$reclength=1;$field="TAB"};
if($cnt==3){$reclength=45;$field="ACCT_CLUS_NM"};
if($cnt==4){$reclength=1;$field="TAB"};
if($cnt==5){$reclength=9;$field="ACCT_NBR"};
if($cnt==6){$reclength=1;$field="TAB"};
if($cnt==7){$reclength=3;$field="CORP_ENT_CD"};
if($cnt==8){$reclength=1;$field="TAB"};
if($cnt==9){$reclength=9;$field="CUSTOMER_NO"};
if($cnt==10){$reclength=1;$field="TAB"};
if($cnt==11){$reclength=20;$field="D_BEN_AGMT_DESC"};
if($cnt==12){$reclength=1;$field="TAB"};
if($cnt==13){$reclength=10;$field="D_BEN_AGMT_EFF_DT"};
if($cnt==14){$reclength=1;$field="TAB"};
if($cnt==15){$reclength=8;$field="D_FUND_TYP_CD"};
if($cnt==16){$reclength=1;$field="TAB"};
if($cnt==17){$reclength=6;$field="D_PROD_TYP_CD"};
if($cnt==18){$reclength=1;$field="TAB"};
if($cnt==19){$reclength=8;$field="FOLDED_FUNDING_TYPE"};
if($cnt==20){$reclength=1;$field="TAB"};
if($cnt==21){$reclength=40;$field="GROUP_NAME"};
if($cnt==22){$reclength=1;$field="TAB"};
if($cnt==23){$reclength=9;$field="GROUP_NO"};
if($cnt==24){$reclength=1;$field="TAB"};
if($cnt==25){$reclength=2;$field="GRP_CAN_CD"};
if($cnt==26){$reclength=1;$field="TAB"};
if($cnt==27){$reclength=10;$field="GRP_CAN_DT"};
if($cnt==28){$reclength=1;$field="TAB"};
if($cnt==29){$reclength=10;$field="GS_ORIGL_EFF_DT"};
if($cnt==30){$reclength=1;$field="TAB"};
if($cnt==31){$reclength=20;$field="H_BEN_AGMT_DESC"};
if($cnt==32){$reclength=1;$field="TAB"};
if($cnt==33){$reclength=10;$field="H_BEN_AGMT_EFF_DT"};
if($cnt==34){$reclength=1;$field="TAB"};
if($cnt==35){$reclength=8;$field="H_FUND_TYP_CD"};
if($cnt==36){$reclength=1;$field="TAB"};
if($cnt==37){$reclength=6;$field="H_PROD_TYP_CD"};
if($cnt==38){$reclength=1;$field="TAB"};
if($cnt==39){$reclength=10;$field="LOAD_DT"};
if($cnt==40){$reclength=1;$field="TAB"};
if($cnt==41){$reclength=10;$field="LST_UPDT_DT"};
if($cnt==42){$reclength=1;$field="TAB"};
if($cnt==43){$reclength=4;$field="MKT_SEG_CD"};
if($cnt==44){$reclength=1;$field="TAB"};
if($cnt==45){$reclength=45;$field="NAME"};
if($cnt==46){$reclength=1;$field="TAB"};
if($cnt==47){$reclength=10;$field="ROW_EFFECTIVE_DT"};
if($cnt==48){$reclength=1;$field="TAB"};
if($cnt==49){$reclength=10;$field="ROW_END_DT"};
if($cnt==50){$reclength=1;$field="TAB"};
if($cnt==51){$reclength=5;$field="SECT_NBR"};
if($cnt==52){$reclength=1;$field="TAB"};
if($cnt==53){$reclength=40;$field="SECT_NM"};
if($cnt==54){$reclength=1;$field="TAB"};
if($cnt==55){$reclength=4;$field="SIC_CD"};
if($cnt==56){$reclength=1;$field="TAB"};
if($cnt==57){$reclength=10;$field="TIER_TYPE"};
chomp($RECIN);
$RECOUT=substr($RECIN,$fpos,$reclength);
$fpos=$reclength + $fpos;
print FBATCHF "$field $reclength |$RECOUT|\n";
}
print FBATCHF "------------------------------------------------\n";
}
close BATCHF;
close FBATCHF;