CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Formmatted output to a file

 



sreenivas
New User

Apr 22, 2014, 5:38 AM

Post #1 of 5 (2531 views)
Formmatted output to a file Can't Post

Hi,
I am generating the report using perl report, where am writing the report on to a file. My file having 2 header fields having the lenght 100 and 2000 respectively. I could able genearate the report in the following format

Filed1 Filed2
Filed1data Filed2data

But I wanted to genearte in below format
PAGE:1
Filed1 Filed2
-------------------------- -------------------------------
Filed1data Filed2data


My code is:
my $errorFile = "error.txt";
open my $outputErrorFile, '>>', $errorFile;
print {$outputErrorFile} sprintf("%-100s%-2000s\n", "Filed1 ", "Filed2");

print {$outputErrorFile} sprintf("%-100s%2000s\n", "Invalid Record length", "some dynamic data goes here");

Please let me know how can I add dashed line under head and page nos and no.of records in each page.

Thanks in advance


(This post was edited by sreenivas on Apr 22, 2014, 5:51 AM)


BillKSmith
Veteran

Apr 22, 2014, 6:28 AM

Post #2 of 5 (2526 views)
Re: [sreenivas] Formmatted output to a file [In reply to] Can't Post


Code
print {$outputErrorFile} '-' x 2100, "\n";


Also note: You are using fixed length fields without any field separators. The function sprintf will will write these as long as all goes well. However, it will extend a field if you try to output to many characters. The function pack would enforce your fixed length format.
Good Luck,
Bill


sreenivas
New User

Apr 23, 2014, 5:12 AM

Post #3 of 5 (2517 views)
Re: [BillKSmith] Formmatted output to a file [In reply to] Can't Post

Thanks Bill for your help, but how can I set page nos, I want to print 30 records in each page.


FishMonger
Veteran / Moderator

Apr 23, 2014, 6:35 AM

Post #4 of 5 (2495 views)
Re: [sreenivas] Formmatted output to a file [In reply to] Can't Post

What you're looking for is \f which is the form feed character. So, after every 30th record, print that char and your 3 line header.


(This post was edited by FishMonger on Apr 23, 2014, 6:35 AM)


BillKSmith
Veteran

Apr 23, 2014, 12:25 PM

Post #5 of 5 (2476 views)
Re: [sreenivas] Formmatted output to a file [In reply to] Can't Post

Here are a few questions to consider while developing the initialization phase of FishMonger's algorithm.

You are appending to an existing file. Where are you going to get the number of your first page? From the file? Do you have to finish the last page of the existing file before starting a new page?
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives