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

Code to display output

Former Member
0 Likes
1,008

Hi ,

I have an itab with fields f1,f2,f3 and f4.

I have the values as below:

itab-row1 = 101 US de91 peter

itab-row2 = 101 US de92 cathy

I must show the output like this ..

101

US

<item>

de91

peter

</item>

<item>

de92

cathy

</item>

I can do this code to just put all the rows:

loop at itab.

write 😕 itab-f1.

write 😕 itab-f2.

write 😕 itab-f3.

write 😕 itab-f4.

endloop.

But how can do the above output to avoid the f1 & f2.

Please tell me the logic how I can do this

Thanks in advance.

Message was edited by:

ramana peddu

1 ACCEPTED SOLUTION
Read only

ferry_lianto
Active Contributor
0 Likes
984

Hi,

Please try this.


DATA: WA_ITAB LIKE ITAB.

SORT ITAB.
LOOP AT ITAB INTO WA_ITAB.
  AT NEW f1.
    WRITE: / WA_ITAB-F1,
           / WA_ITAB-F2.
  ENDAT.

  WRITE: / WA_ITAB-F3,
         / WA_ITAB-F4.
ENDLOOP.

Regards,

Ferry Lianto

8 REPLIES 8
Read only

Former Member
0 Likes
984

You can use something like this:

LOOP AT ITAB.

AT NEW f1.

WRITE: / itab-f1,

/ itab-f2.

ENDAT.

WRITE: / itab-f3,

/ itab-f4.

ENDLOOP.

This should only write out the field 1 and field 2 values when field 1 changes.

- April King

Read only

0 Likes
984

When I do this ...

the data in the itab is like this ..whenever it crosses the line AT NEW f1.

itab-f1 = 101.

itab-f2 = ***.

itab-f3 = ***.

itab-f4 = ***.

Anyone please help me ASAP

Read only

0 Likes
984

Small correction to April Kings code.

LOOP AT ITAB.
AT NEW f1.
read table itab index sy-index.
if sy-subrc = 0.
WRITE: / itab-f1,
/ itab-f2.
ENDAT.
WRITE: / itab-f3,
/ itab-f4.
endif.
endat.
ENDLOOP.

Read only

0 Likes
984

Okay. You could try saving the field 1 and field 2 data in comparison fields instead of using the AT NEW. Like this:

data: save_field1 TYPE field1,

save_field2 TYPE field2.

LOOP AT itab.

IF itab-field1 <> save_field1.

WRITE: / itab-field1.

MOVE itab-field1 TO save_field1.

ENDIF.

IF itab-field2 <> save_field2.

WRITE: / itab-field2.

MOVE itab-field2 TO save_field2.

ENDIF.

WRITE: / itab-field3,

/ itab-field4.

ENDLOOP.

- April King

Read only

ferry_lianto
Active Contributor
0 Likes
985

Hi,

Please try this.


DATA: WA_ITAB LIKE ITAB.

SORT ITAB.
LOOP AT ITAB INTO WA_ITAB.
  AT NEW f1.
    WRITE: / WA_ITAB-F1,
           / WA_ITAB-F2.
  ENDAT.

  WRITE: / WA_ITAB-F3,
         / WA_ITAB-F4.
ENDLOOP.

Regards,

Ferry Lianto

Read only

0 Likes
984

When I do this ...

the data in the itab is like this ..whenever it crosses the line AT NEW f1.

wa_itab-f1 = 101.

wa_itab-f2 = ***.

wa_itab-f3 = ***.

wa_itab-f4 = ***.

Anyone please help me ASAP

Read only

0 Likes
984

Hi ramana,

Did you see my reply?

YOu have to read the table again once you come into the at new block.

LOOP AT ITAB.
AT NEW f1.
read table itab index sy-index.
if sy-subrc = 0.
WRITE: / itab-f1,
...........
.

WRITE: / itab-f3,
/ itab-f4.
endif.
endat.
ENDLOOP.

Read only

Former Member
0 Likes
984

Try using this code.

DATA: w_itab LIKE itab.

SORT itab BY f1 f2.

LOOP AT itab INTO w_itab.

  AT NEW f2.
    WRITE:/2 w_itab-f1.
    WRITE:/2 w_itab-f2.
  ENDAT.

  SKIP 1.
  WRITE:/2 w_itab-f3.
  WRITE:/2 w_itab-f4.

ENDLOOP.