tags:

views:

75

answers:

1

So far, I've been successful with generating output to individual files by opening a file for output as part of outer loop and closing it after all output is written. I had used a counting variable ($x) and appended .txt onto it to create a filename, and had written it to the same directory as my perl script. I want to step the code up a bit, prompt for a file name from the user, open that file once and only once, and write my output one "printed letter" per page. Is this possible in plain text? From what I understand, chr(12) is an ascii line feed character and will get me close to what I want, but is there a better way? Thanks in advance, guys. :)

sub PersonalizeLetters{
    print "\n\n Beginning finalization of letters...";
    print "\n\n I need a filename to save these letters to.";
    print "\n Filename > ";
    $OutFileName = <stdin>;
    chomp ($OutFileName);
    open(OutFile, ">$OutFileName");                 
    for ($x=0; $x<$NumRecords; $x++){
        $xIndex = (6 * $x);
        $clTitle = @ClientAoA[$xIndex];
        $clName = @ClientAoA[$xIndex+1];        
        #I use this 6x multiplier because my records have 6 elements.
        #For this routine I'm only interested in name and title.
        #Reset OutLetter array
        #Midletter has other merged fields that aren't specific to who's receiving the letter.      
        @OutLetter = @MiddleLetter;
        for ($y=0; $y<=$ifLength; $y++){
            #Step through line by line and insert the name.
        $WorkLine = @OutLetter[$y];                 
        $WorkLine =~ s/\[ClientTitle\]/$clTitle/;
        $WorkLine =~ s/\[ClientName\]/$clName/;
        @OutLetter[$y] = $WorkLine;

        }           

        print OutFile "@OutLetter";
        #Will chr(12) work here, or is there something better?
        print OutFile chr(12);
        $StatusX = $x+1;
        print "Writing output $StatusX of $NumRecords... \n\n";
    }
    close(OutFile);
}
+1  A: 

Separate the "pages" with form feeds, but you have to do it after each page, not at the end. I'm not sure what PersonalizeLetters is supposed to do, but it looks like you what to use it to print all of the letters. In that case, I think you just need to restructure it a bit. Do all the setup outside the subroutine, pass in the filename, then do what you need to do for each record. After you process a record, print the form feed:

sub PersonalizeLetters
    {
    my( $OutFileName ) = @_;

    open my $out, '>', $OutFileName 
        or die "Could not open $OutFileName: $!";                 

    for( $x=0; $x < $NumRecords; $x++ )
        {
        print "Writing output $x of $NumRecords...\n\n";
        print $out $stuff_for_this_record;
        print $out "\f";
        }           

    }
brian d foy