Hello Patricia.

Patricia E Gorden-Ozgul wrote:
> > From: Gorden-Ozgul, Patricia E
> > Sent: Tuesday, March 18, 2003 3:36 PM
> > To: '[EMAIL PROTECTED]'
> > Subject: Data Read/Loop/Print Problem
> >
> > I need to modify my code to test the value stored in the 8th
> > position of the input.
> >
> > Here's the input file:
> >
> > ...
> > ...
> > 04500|04500|61.89|1|182988|20021023|61.89|1|0|0000070001|17893|FIRM|
> > 06156|06156|161.92|1|183774|20021115|566.72|3|0|0000070001|20591|FIRM|
> > 06277|06277|323.84|2|183774|20021115|566.72|3|0|0000070001|20591|FIRM|
> > 06265|06265|80.96|3|183774|20021115|566.72|3|0|0000070001|20591|FIRM|
> > 09310|09310|94.46|1|183719|20021126|165.52|2|0|0000070001|22532|FIRM|
> > 09310|09310|71.06|2|183719|20021126|165.52|2|0|0000070001|22532|FIRM|
> >
> >
> > If the value is 1, after printing the 1st and 2nd printf
> > statements, pass it through the 3rd printf statement, producting 3
> > output records.
> >
> > If it is 2, after printing the 1st and 2nd printf statements, pass
> > it and the next input record through the 3rd printf statement,
> > producing 4 output records.
> >
> > If it is 3, after printing the 1st and 2nd printf statements, pass
> > it and the next 2 input records through the 3rd printf statment,
> > producing 5 output records.
> >
> > ...and so on.
> >
> > Here's my code:
> >
> > #!/usr/bin/perl -w
> > #
> > # Parse input from invoice file and split into three output files
> > # PGO March 2003
> > # must run inv_sed against datafile (file9.final) to remove $
> >
> > my $datafile = "invshrt";
> > open DATA, "< $datafile" || die "Can't open data file: $datafile";
> > # my $date = `date +%Y%m%d`;
> >
> > my $date = do {
> >     my @ymd=(localtime(time))[5,4,3];
> >     $ymd[0] += 1900;
> >     $ymd[1] += 1;
> >     sprintf "%04d%02d%02d", @ymd;
> > };
> >
> > while(<DATA>) {
> >     chomp;
> >     my @data = split('\|');
> >     printf "%-2s%-16s%-8s%-10s%-10s%-7s%-11s%-152s%-10s%-16s%-2s\n",
> >         "",
> >         $data[4],
> >         $data[5],
> >         $data[9],
> >         "",
> >         "",
> >         $date,
> >         "",
> >         $data[6],
> >         "",
> >         "01";
> >     printf "%-2s%-10s%-10s%-10s%-4s%-12s%-10s%-184s%-2s\n",
> >         "",
> >         $data[11],
> >         "",
> >         $data[10],
> >         "",
> >         $data[6],
> >         $data[7],
> >         "",
> >         "02";
> >     printf "%-2s%-10s%-6s%-10s%-10s%-8s%-6s%-10s%-180s%-2s\n",
> >         "",
> >         $data[11],
> >         "500500",
> >         $data[2],
> >         $data[10],
> >         $date,
> >         $data[1],
> >         "SPMAT340",
> >         "",
> >         "03";
> > }
> > close DATA;
> > exit 1;
> >
> > Perhaps some code before the 3rd printf akin to (pseudo-code
> > follows): ...
> > ...
> > read
> > store in hash
> > ...
> > printf (the 1st);
> > printf (the 2nd);
> > ...
> > if $data[7] = 1
> > printf (the 3rd );
> > else if $data[7] = 2
> > printf (the 3rd);
> > read next input rec;
> > else if $data[7] = 3;
> > printf (the 3rd);
> > read next input rec;
> > printf (the 3rd);
> > read next input rec;
> > printf (the 3rd);
> >
> I suspect I need to initially read all my data into the hash up
> front, in order to do what's necessary.  I'd need to be able to know
> which record I was on in order to read the next.
>
> So how would I read the entire input file into a hash and print where
> appropriate.

A hash isn't appropriate for the structure of the data
that you have. First of all I'd tidy things up a little by
writing printf1, printf2 and printf3 like this:

    sub printf1 {

        my @data = @_;

        printf "%-2s%-16s%-8s%-10s%-10s%-7s%-11s%-152s%-10s%-16s%-2s\n",
                "", $data[4], $data[5], $data[9], "", "", $date, "", $data[6], "", 
"01";
    }

and so on. Then you can set up a variable for the record
count you get from field 8 of the first record and count
through the subsequent file reads, like this:

    my $records = 0;

    while(<DATA>) {

        chomp;
        my @data = split /|/;

        if ($records == 0) {
            printf1(@data);
            printf2(@data);
            $records = $data[7];
        }

        printf3(@data);
        $records--;
    }

I think that's straightforward. Come back if you have
any problems.

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to