parsing a variable length record from a mixed format file

Discussion in 'Perl Misc' started by chadmay@hotmail.com, May 8, 2007.

  1. Guest

    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;
    , May 8, 2007
    #1
    1. Advertising

  2. Greg Bacon Guest

    In article <>,
    <> wrote:

    : 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??

    Are the fields you tagged with "TAB" literal TAB characters? If so,
    extracting the fields is easy:

    @fields = split /\t/, $RECIN;

    Hope this helps,
    Greg
    --
    Every election is a sort of advance auction sale of stolen goods.
    -- H. L. Mencken
    Greg Bacon, May 8, 2007
    #2
    1. Advertising

  3. Guest

    On May 8, 11:10 am, (Greg Bacon) wrote:
    > In article <>,
    > <> wrote:
    >
    > : 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??
    >
    > Are the fields you tagged with "TAB" literal TAB characters? If so,
    > extracting the fields is easy:
    >
    > @fields = split /\t/, $RECIN;
    >
    > Hope this helps,
    > Greg
    > --
    > Every election is a sort of advance auction sale of stolen goods.
    > -- H. L. Mencken


    You are correct, those are literal TABS that I've specificly listed in
    the record layout. Since I'm somewhat of a PERL newbie, I want to make
    sure I'm not missing anything. Where would I insert the line you
    proposed and what other changes to my current code, do I need to make?

    Thanks again,
    Chad
    , May 8, 2007
    #3
  4. tuser Guest

    On May 8, 5:41 pm, wrote:
    > 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??


    I am not sure if I understand your question, but I suggest the
    following changes to your program:

    > #!/usr/bin/perl


    use strict;
    use warnings;

    > $loop=57;
    > $ifname=$ARGV[0];
    > $ofname=$ARGV[1];


    my $loop = 57;
    my $ifname = $ARGV[0];
    my $ofname = $ARGV[1];

    > if("${ifname}" eq "" | "${ofname}" eq "")

    if ($ifname eq "" or $ofname eq "")

    no need for curly braces here and no need to quote $ifname or $ofname,
    see perlfaq4 (What's wrong with always quoting "$vars" ?).

    use "or" instead of "|", see perldoc perlop ("Bitwise Or and Exclusive
    Or" and "C-style Logical Or").

    > {
    > print "\nUsage: $0 input_file output_file\n";
    > exit;}
    >
    > if(!-f "$ifname" | !-r "$ifname")


    if (!-f $ifname or !-r "$ifname")

    no curlies, that's good, but again, "or" instead of "|" and no need to
    quote $ifname.

    > {
    > print "$ifname doesn't exist or you do not have permission to read
    > it.\n";
    > exit 1;}
    >
    > if(-f "$ofname")


    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";


    open my $BATCHF, '<', $ifname or die "Open < $ifname failed: $!\n";
    open my $FBATCHF, '>>', $ofname or die "Open >> $ofname failed: $!\n";

    > while (<BATCHF>)


    while (<$BATCHF>)

    [ ...snip... ]

    > for ($cnt=1;$cnt<=$loop;$cnt++)


    for my $cnt (1..$loop)

    [ ...snip... ]

    > print FBATCHF "$field $reclength |$RECOUT|\n";


    print {$FBATCHF} "$field $reclength |$RECOUT|\n";

    > close BATCHF;
    > close FBATCHF;


    close $BATCHF;
    close $FBATCHF;

    --
    Klaus
    tuser, May 8, 2007
    #4
  5. wrote:
    > 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??


    It looks like this may be close to what you want:

    #!/usr/bin/perl
    use warnings;
    use strict;

    @ARGV == 2 or die "\nUsage: $0 input_file output_file\n";

    my ( $ifname, $ofname ) = @ARGV;

    my @fields = qw(
    ACCOUNT_TYPE
    ACCT_CLUS_NM
    ACCT_NBR
    CORP_ENT_CD
    CUSTOMER_NO
    D_BEN_AGMT_DESC
    D_BEN_AGMT_EFF_DT
    D_FUND_TYP_CD
    D_PROD_TYP_CD
    FOLDED_FUNDING_TYPE
    GROUP_NAME
    GROUP_NO
    GRP_CAN_CD
    GRP_CAN_DT
    GS_ORIGL_EFF_DT
    H_BEN_AGMT_DESC
    H_BEN_AGMT_EFF_DT
    H_FUND_TYP_CD
    H_PROD_TYP_CD
    LOAD_DT
    LST_UPDT_DT
    MKT_SEG_CD
    NAME
    ROW_EFFECTIVE_DT
    ROW_END_DT
    SECT_NBR
    SECT_NM
    SIC_CD
    TIER_TYPE
    );

    open BATCHF, '<', $ifname or die "Open '$ifname' failed: $!\n";
    open FBATCHF, '>>', $ofname or die "Open '$ofname' failed: $!\n";

    while ( my $RECIN = <BATCHF> ) {
    chomp $RECIN;
    @records = split /\t/, $RECIN, -1;
    print FBATCHF map( "$fields[$_] " . length( $records[ $_ ] ) . "
    |$records[$_]|\n", 0 .. $#fields ), '-' x 48, "\n";
    }

    close BATCHF;
    close FBATCHF;









    >
    > #!/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;
    >








    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, May 8, 2007
    #5
  6. Greg Bacon Guest

    In article <>,
    <> wrote:

    : You are correct, those are literal TABS that I've specificly listed in
    : the record layout. Since I'm somewhat of a PERL newbie, I want to make
    : sure I'm not missing anything. Where would I insert the line you
    : proposed and what other changes to my current code, do I need to make?

    John W. Krahn's followup seems to have a reasonable approach.
    Has it made it to your news server? Does it help?

    Greg
    --
    Every kind of peaceful cooperation among men is primarily based on
    mutual trust and only secondarily on institutions such as courts of
    justice and police.
    -- Albert Einstein
    Greg Bacon, May 8, 2007
    #6
  7. On May 8, 4:41 pm, wrote:
    > 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??


    You cannot code for something you can't even describe. If you have a
    record with a mix of fixed length and variable length fields you need
    some property of the record that lets you determine how wide the
    variable fields are. Since you've not described how you (a human)
    could tell how long the ACCT_CLUS_NM field is in a given record
    there's no way you can convert that lack of knowledge into working
    code.

    Sorry - I've just re-read this thread and realised this is a simple
    tab delimited record, so you should simply split() it. (I was going to
    tell you about unpack() and m//gc as approaches to mixed fixed/
    variable with fields but neither of these are now relevant).

    >
    > #!/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;}


    Please see other people's comments on cleaning up your Perl. In
    particular decalre all variables as lexically scoped in the smallest
    applicable scope unless there is a reason not to (this is not specific
    to Perl). You can then tell perl to insist on explicit declaration. It
    really will save you a lot of pain in future. When learning to climb
    all those ropes do get in the way but after the first couple of slips
    the effort pays off.

    > if(!-f "$ifname" | !-r "$ifname")
    > {
    > print "$ifname doesn't exist or you do not have permission to read
    > it.\n";
    > exit 1;}


    Remove that block of code - the error handling on open() already does
    a better job of telling the user what's actually wrong.

    > if(-f "$ofname")
    > {
    > print "$ofname already exists.\n";
    > exit 1;
    >
    > }


    > open(FBATCHF, ">> $ofname") or die "Open failed: $!\n";


    Why are you trying to append to a file that you've just checked does
    not already exist? This won't confuse the computer but it sure does
    confuse any human who looks at your code.

    > if($cnt==1){$reclength=4;$field="ACCOUNT_TYPE"};
    > if($cnt==2){$reclength=1;$field="TAB"};


    > if($cnt==56){$reclength=1;$field="TAB"};
    > if($cnt==57){$reclength=10;$field="TIER_TYPE"};


    If you are doing the same thing 3 times you are probably doing it
    wrong. You are doing the same thing 57 times. The word "array" should
    be burning so brightly in your mind's eye that smoke should be issuing
    from your ears.
    Brian McCauley, May 9, 2007
    #7
  8. Guest

    On May 9, 2:06 am, Brian McCauley <> wrote:
    > On May 8, 4:41 pm, wrote:
    >
    > > 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??

    >
    > You cannot code for something you can't even describe. If you have a
    > record with a mix of fixed length and variable length fields you need
    > some property of the record that lets you determine how wide the
    > variable fields are. Since you've not described how you (a human)
    > could tell how long the ACCT_CLUS_NM field is in a given record
    > there's no way you can convert that lack of knowledge into working
    > code.
    >
    > Sorry - I've just re-read this thread and realised this is a simple
    > tab delimited record, so you should simply split() it. (I was going to
    > tell you about unpack() and m//gc as approaches to mixed fixed/
    > variable with fields but neither of these are now relevant).
    >
    >
    >
    > > #!/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;}

    >
    > Please see other people's comments on cleaning up your Perl. In
    > particular decalre all variables as lexically scoped in the smallest
    > applicable scope unless there is a reason not to (this is not specific
    > to Perl). You can then tell perl to insist on explicit declaration. It
    > really will save you a lot of pain in future. When learning to climb
    > all those ropes do get in the way but after the first couple of slips
    > the effort pays off.
    >
    > > if(!-f "$ifname" | !-r "$ifname")
    > > {
    > > print "$ifname doesn't exist or you do not have permission to read
    > > it.\n";
    > > exit 1;}

    >
    > Remove that block of code - the error handling on open() already does
    > a better job of telling the user what's actually wrong.
    >
    > > if(-f "$ofname")
    > > {
    > > print "$ofname already exists.\n";
    > > exit 1;

    >
    > > }
    > > open(FBATCHF, ">> $ofname") or die "Open failed: $!\n";

    >
    > Why are you trying to append to a file that you've just checked does
    > not already exist? This won't confuse the computer but it sure does
    > confuse any human who looks at your code.
    >
    > > if($cnt==1){$reclength=4;$field="ACCOUNT_TYPE"};
    > > if($cnt==2){$reclength=1;$field="TAB"};
    > > if($cnt==56){$reclength=1;$field="TAB"};
    > > if($cnt==57){$reclength=10;$field="TIER_TYPE"};

    >
    > If you are doing the same thing 3 times you are probably doing it
    > wrong. You are doing the same thing 57 times. The word "array" should
    > be burning so brightly in your mind's eye that smoke should be issuing
    > from your ears.
    , May 9, 2007
    #8
  9. Guest

    On May 8, 2:43 pm, "John W. Krahn" <> wrote:
    > wrote:
    > > 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??

    >
    > It looks like this may be close to what you want:
    >
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > @ARGV == 2 or die "\nUsage: $0 input_file output_file\n";
    >
    > my ( $ifname, $ofname ) = @ARGV;
    >
    > my @fields = qw(
    > ACCOUNT_TYPE
    > ACCT_CLUS_NM
    > ACCT_NBR
    > CORP_ENT_CD
    > CUSTOMER_NO
    > D_BEN_AGMT_DESC
    > D_BEN_AGMT_EFF_DT
    > D_FUND_TYP_CD
    > D_PROD_TYP_CD
    > FOLDED_FUNDING_TYPE
    > GROUP_NAME
    > GROUP_NO
    > GRP_CAN_CD
    > GRP_CAN_DT
    > GS_ORIGL_EFF_DT
    > H_BEN_AGMT_DESC
    > H_BEN_AGMT_EFF_DT
    > H_FUND_TYP_CD
    > H_PROD_TYP_CD
    > LOAD_DT
    > LST_UPDT_DT
    > MKT_SEG_CD
    > NAME
    > ROW_EFFECTIVE_DT
    > ROW_END_DT
    > SECT_NBR
    > SECT_NM
    > SIC_CD
    > TIER_TYPE
    > );
    >
    > open BATCHF, '<', $ifname or die "Open '$ifname' failed: $!\n";
    > open FBATCHF, '>>', $ofname or die "Open '$ofname' failed: $!\n";
    >
    > while ( my $RECIN = <BATCHF> ) {
    > chomp $RECIN;
    > @records = split /\t/, $RECIN, -1;
    > print FBATCHF map( "$fields[$_] " . length( $records[ $_ ] ) . "
    > |$records[$_]|\n", 0 .. $#fields ), '-' x 48, "\n";
    > }
    >
    > close BATCHF;
    > close FBATCHF;
    >
    >
    >
    >
    >
    >
    >
    > > #!/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;

    >
    > John
    > --
    > Perl isn't a toolbox, but a small machine shop where you can special-order
    > certain sorts of tools at low cost and in short order. -- Larry Wall- Hide quoted text -
    >
    > - Show quoted text -


    Problem is solved now, Thanks John!
    , May 9, 2007
    #9
  10. wrote:
    [ ... ]
    > Problem is solved now, Thanks John!


    What was the reason to quote the entire thread just to add a single line?

    Just curious,

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, May 10, 2007
    #10
  11. wrote:

    [ ... ]

    Yes ... and?

    Just curious,

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, May 10, 2007
    #11
  12. Josef Moellers <> wrote:
    > wrote:
    > [ ... ]
    >> Problem is solved now, Thanks John!

    >
    > What was the reason to quote the entire thread just to add a single line?



    To cut down on the number of articles that I need to read.

    :-(


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 10, 2007
    #12
  13. Dave Weaver <> wrote:
    > On 8 May 2007 12:36:42 -0700, tuser <> wrote:
    >> On May 8, 5:41 pm, wrote:
    >> >
    >> > if(!-f "$ifname" | !-r "$ifname")

    >>
    >> if (!-f $ifname or !-r "$ifname")
    >>
    >> no curlies, that's good, but again, "or" instead of "|" and no need to
    >> quote $ifname.

    >
    > In a bid for even more readability, I'd write the above as
    >
    > if ( not -f $ifname or not -r $ifname ) {



    In a bid for faster execution, I'd write the above as

    if ( not -f $ifname or not -r _ ) {


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 22, 2007
    #13
  14. Dr.Ruud Guest

    Tad McClellan schreef:

    > if ( not -f $ifname or not -r _ ) {


    Variant:

    unless ( -f $ifname and -r _ ) {

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, May 22, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    24
    Views:
    1,096
  2. Panchal V
    Replies:
    18
    Views:
    755
    Mark A. Odell
    Feb 4, 2004
  3. cksanjose

    Reading Fixed Length Record File

    cksanjose, Oct 27, 2006, in forum: Java
    Replies:
    3
    Views:
    650
  4. Chris McMahon
    Replies:
    0
    Views:
    73
    Chris McMahon
    Mar 26, 2006
  5. Randy Kramer
    Replies:
    2
    Views:
    389
    Randy Kramer
    Jan 12, 2007
Loading...

Share This Page