Solved

Printing a Report using Bytes

Posted on 2003-11-25
4
318 Views
Last Modified: 2010-03-04
Hi All,
I am having a problem and i need to print a report using PERL. Now problem is this that every data which i am printing in that must have specified byte.

Say - I am having A,B,C,D,E,F,G,H and Icolumn which need to to be printed in a flat file and it is having following requirements -

Column Name     Length     Position     Type
A                         1             1               Char
B                         11            2              Integer  
C                         10            13            Char
D                         8              23             Char
E                         1               31            Char
F                         150          32             Varchar
G                        8              182            Date
H                        40             190           Varchar
I                         8              230              Date

Now every record length must be 238 bytes.
I am not having idea how can we do this. Can somebody help me in this ? We have to print this report in a while loop and every line of record must have 238 bytes.

Thanks in advance.....

Akku
0
Comment
Question by:akku_batra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Accepted Solution

by:
terageek earned 30 total points
ID: 9821566
See the following documentation:

perldoc -f write
perldoc -f format
man perlform

Just about any book on Perl will have a chapter on formats.

You will want something like this...

format REPORT =
@@<<<<<<<<<<@<<<<<<<<<@<<<<<<<@@(149 <'s)@<<<<<<<@(40 <'s)@<<<<<<<
$A, $B, $C, $D, $E, $F, $G, $H, $I
.

open (REPORT, ">output.file");

while (yadda yadda yadda) {
    ....
    # Set $A - $I
    write REPORT;
}
0
 
LVL 18

Expert Comment

by:kandura
ID: 9821777
you could also use pack for this:

$record = pack "A1 A11 A10 A8 A1 A150 A8 A40 A8 " , @your_fields;

Note that you can also put the numbers in [ ] when using perl 5.8, which is a bit more readable:

$record = pack "A[1] A[11] A[10] A[8] A[1] A[150] A[8] A[40] A[8] ", @your_fields;

The A means that it's a string, padded with spaces if necessary.

0
 
LVL 20

Expert Comment

by:jmcg
ID: 10093730
Nothing has happened on this question in more than 5 weeks. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by terageek (too few points to split, but kandura's answer is also good).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question