Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Fixed Length Output

Former Member
0 Likes
2,861

Hi,

Newbie here with a question. I need to output a file based on a fixed length i.e the data in an internal table looks like this

abc123

xyz456789

I have a parameter on the selection screen defining charachter length, if the parameter is set to 10, the output needs to be 10 charachters long i.e

abc123____,

xyz456789_,

_ represents a space.

Can anybody suggest how I do this.

Thanks

Jez

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,101

Hi Jez,

OK. Try this:

PARAMETER: P_LE(2) TYPE N DEFAULT 06.

DATA: DATEI_A(30) TYPE C VALUE '/tmp/tmp.txt'.

*

DATA: CHAR(20).

DATA: LEN TYPE I.

DATA: BEGIN OF ITAB OCCURS 0,

TXT1(20),

END OF ITAB.

*

ITAB-TXT1 = '1234567890'. APPEND ITAB.

ITAB-TXT1 = '123456'. APPEND ITAB.

ITAB-TXT1 = '123'. APPEND ITAB.

*

*

OPEN DATASET DATEI_A FOR OUTPUT IN TEXT MODE.

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

LOOP AT ITAB.

*

LEN = STRLEN( ITAB-TXT1 ).

IF LEN > P_LE.

CHAR = ITAB-TXT1(P_LE).

TRANSFER CHAR TO DATEI_A.

CHAR = ITAB-TXT1+P_LE(P_LE).

TRANSFER CHAR TO DATEI_A.

ELSE.

CHAR = ITAB-TXT1.

TRANSFER CHAR TO DATEI_A.

ENDIF.

*

ENDLOOP.

*

CLOSE DATASET DATEI_A.

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

Regards, Dieter

13 REPLIES 13
Read only

Former Member
0 Likes
2,101

Hi Jez,

you can set the position on the output like this:

parameter: p_le(2) type n default 10.

data: begin of itab occurs 0,

txt1(6),

txt2(8),

end of itab.

*

itab-txt1 = 'first1'. itab-txt2 = 'first2'. append itab.

itab-txt1 = 'sec1'. itab-txt2 = 'sec2'. append itab.

itab-txt1 = 'third1'. itab-txt2 = 'third2'. append itab.

*

loop at itab.

write: / itab-txt1, at p_le itab-txt2.

endloop.

Regards, Dieter

Read only

Former Member
0 Likes
2,101

Hi,

Try using WRITE AT specifying the lentgh with respect to the parameter.

LOOP AT ITAB.

WRITE AT: /(p_length) itab-char .

ENDLOOP.

Award points if helpful.

Regards,

LM

Read only

Former Member
0 Likes
2,101

Hi,

Parameter: Var type I.

LOOP AT ITAB.

write:/(VAR) ITAB_FIELD1.

ENDLOOP.

so whatever you enter in the VAR, that many charecters will be printed .....

Regards

Sudheer

Read only

0 Likes
2,101

Sorry, I think I didn't phrase my question properly. I am not using write, but the Transfer statement.

Read only

0 Likes
2,101

PARAMETERS : P_LENGTH TYPE I DEFAULT 10.

1. Declare one char of length 1000
     DATA : TRANSFER_STRUCTURE(1000).

2. LOOP AT ITAB.

           WRITE :  ITAB-FIELD1 TO  TRANSFER_STRUCTURE+0( P_LENGTH ),
                    ITAB-FIELD1 TO  TRANSFER_STRUCTURE+10( P_LENGTH ).
  
   ENDLOOP.

3. Then use TRANSFER statement
 
     TRANFER    TRANSFER_STRUCTURE  TO  W_FNAME.

Message was edited by:

Chandrasekhar Jagarlamudi

Read only

Former Member
0 Likes
2,101

Hi Jez,

try this:

PARAMETER: P_LE(2) TYPE N DEFAULT 10.

*

DATA: DATEI_A(30) TYPE C VALUE '/tmp/tmp.txt'.

*

DATA: CHAR(255).

*

DATA: BEGIN OF ITAB OCCURS 0,

TXT1(6),

TXT2(8),

END OF ITAB.

*

ITAB-TXT1 = 'first1'. ITAB-TXT2 = 'first2'. APPEND ITAB.

ITAB-TXT1 = 'sec1'. ITAB-TXT2 = 'sec2'. APPEND ITAB.

ITAB-TXT1 = 'third1'. ITAB-TXT2 = 'third2'. APPEND ITAB.

*

*

OPEN DATASET DATEI_A FOR OUTPUT IN TEXT MODE.

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

LOOP AT ITAB.

*

CHAR+0(P_LE) = ITAB-TXT1.

CHAR+P_LE(P_LE) = ITAB-TXT2.

TRANSFER char TO DATEI_A.

*

ENDLOOP.

*

CLOSE DATASET DATEI_A.

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

Regards, Dieter

Read only

0 Likes
2,101

Thanks, but still not quite right, I will try to explain better.

If I have the following data series

1234567890

123456

123

I I set the parameter for the length to be 6, the output I want is

123456

7890__

123456

123___

Where _ represents a space. The fields need to be bulked out the the specified length with spaces if too short, and if too long, the length needs be cut to the entered parameter, and anything after written to the next line.

Hope this makes it a bit clearer. Thanks for all the suggestions so far.

Read only

0 Likes
2,101

Hi Jez,

Check out this following code. Just replace the WRITE statement with TRANSFER.

DATA: v_var1(21) TYPE c,

v_var2(5) TYPE c,

v_var3(5) TYPE c,

v_var4 TYPE i.

DATA: len TYPE i,

c_len(2) TYPE c VALUE '5',

v_len(2) TYPE c,

v_string TYPE string.

v_var1 = 'ABCDEFGHIJKLMNOPQRSTU'.

v_string = v_var1.

len = STRLEN( v_string ).

v_var3 = len MOD c_len.

IF v_var3 <> 0.

v_var4 = len / c_len.

v_var4 = v_var4 + 1.

ELSE.

v_var4 = len / c_len.

ENDIF.

CLEAR v_len.

DO v_var4 TIMES.

v_var2 = v_string+v_len.

WRITE:/ v_var2.

v_len = v_len + c_len.

ENDDO.

Ashven

Message was edited by:

Ashvender Kumar

Read only

Former Member
0 Likes
2,102

Hi Jez,

OK. Try this:

PARAMETER: P_LE(2) TYPE N DEFAULT 06.

DATA: DATEI_A(30) TYPE C VALUE '/tmp/tmp.txt'.

*

DATA: CHAR(20).

DATA: LEN TYPE I.

DATA: BEGIN OF ITAB OCCURS 0,

TXT1(20),

END OF ITAB.

*

ITAB-TXT1 = '1234567890'. APPEND ITAB.

ITAB-TXT1 = '123456'. APPEND ITAB.

ITAB-TXT1 = '123'. APPEND ITAB.

*

*

OPEN DATASET DATEI_A FOR OUTPUT IN TEXT MODE.

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

LOOP AT ITAB.

*

LEN = STRLEN( ITAB-TXT1 ).

IF LEN > P_LE.

CHAR = ITAB-TXT1(P_LE).

TRANSFER CHAR TO DATEI_A.

CHAR = ITAB-TXT1+P_LE(P_LE).

TRANSFER CHAR TO DATEI_A.

ELSE.

CHAR = ITAB-TXT1.

TRANSFER CHAR TO DATEI_A.

ENDIF.

*

ENDLOOP.

*

CLOSE DATASET DATEI_A.

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

Regards, Dieter

Read only

0 Likes
2,101

Nearly there, but it doesn't add the spaces on the end of the 7890.

Read only

0 Likes
2,101

Hi Jaz,

Before transferring the field do like this:

DATA: v_var2(5) TYPE c.

v_var2 = v_string+v_len.

Transfer v_var2 to file.

This will write spaces.

If you have two or three field in a string before transferring to application server the concatenate the fields into string using respecting blanks addition:

e.g.:

CONCATENATE x_itext-docno x_itext-purodr x_itext-date

x_itext-invno x_itext-amount x_itext-disc

x_itext-netamt

INTO v_string2

RESPECTING BLANKS.

For confiramtion copy the following code in tepm program & execute it. weateher it is working or not?

ATA: v_var1(21) TYPE c,

v_var2(5) TYPE c,

v_var3(5) TYPE c,

v_var4 TYPE i.

DATA: len TYPE i,

c_len(2) TYPE c VALUE '5',

v_len(2) TYPE c,

v_string TYPE string.

v_var1 = 'ABCDEFGHIJKLMNOPQRSTU'.

v_string = v_var1.

len = STRLEN( v_string ).

v_var3 = len MOD c_len.

IF v_var3 <> 0.

v_var4 = len / c_len.

v_var4 = v_var4 + 1.

ELSE.

v_var4 = len / c_len.

ENDIF.

CLEAR v_len.

DO v_var4 TIMES.

v_var2 = v_string+v_len.

WRITE:/ v_var2 , 'ABC'.

v_len = v_len + c_len.

ENDDO.

Ashvender.

Message was edited by:

Ashvender Kumar

Read only

Former Member
0 Likes
2,101

REPORT zex24 .

PARAMETER : p_len TYPE i DEFAULT 10.

DATA : BEGIN OF itab OCCURS 0,

f1(30),

END OF itab.

itab-f1 = 'abc123'.

APPEND itab.

CLEAR itab.

itab-f1 = 'xyz456789'.

APPEND itab.

CLEAR itab.

itab-f1 = 'ksjfhasjsa544332'.

APPEND itab.

CLEAR itab.

LOOP AT itab.

WRITE : / itab-f1+0(p_len).

ENDLOOP.

Read only

Former Member
0 Likes
2,101

Hi Jez,

Were you able to get the correct answer?

I can do the space at the end using WS_DOWNLOAD but cant do a solution for Dataset OPEN or TRANSFER.

Thanks and best regards,

KLL