‎2015 Jan 08 4:39 PM
Hi, i have an internal table "itab" with a lot of fields. table is sorted.
Like this
| Header 1 | Header 2 | Header 3 |
|---|---|---|
| a | 2132 | 123 |
| a | 312 | 123 |
| a | 312 | 312 |
| b | 132 | 132 |
| c | 32 | 123 |
| c | 12 | 13 |
lets say i want the last lines of the different values of Header 1.
in this case, the last one where header 1 is a, then last one where is b, last one where it is c
a 312 312
b 132 132
c 12 13
is there any simple solution?
‎2015 Jan 08 6:59 PM
hi bruno,
Try this code for the solution.
REPORT ZR_TEST2.
TYPES: BEGIN OF TY,
HEADER1 TYPE C LENGTH 10,
HEADER2 TYPE C LENGTH 10,
HEADER3 TYPE C LENGTH 10,
END OF TY.
DATA: WA TYPE TY,
WA1 TYPE TY,
ITAB LIKE STANDARD TABLE OF WA,
ITAB1 LIKE STANDARD TABLE OF WA.
WA-HEADER1 = 'a'.
WA-HEADER2 = '2132'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.
WA-HEADER1 = 'a'.
WA-HEADER2 = '312'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.
WA-HEADER1 = 'a'.
WA-HEADER2 = '312'.
WA-HEADER3 = '312'.
APPEND WA TO ITAB.
CLEAR WA.
WA-HEADER1 = 'b'.
WA-HEADER2 = '132'.
WA-HEADER3 = '132'.
APPEND WA TO ITAB.
CLEAR WA.
WA-HEADER1 = 'c'.
WA-HEADER2 = '32'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.
WA-HEADER1 = 'c'.
WA-HEADER2 = '12'.
WA-HEADER3 = '13'.
APPEND WA TO ITAB.
CLEAR WA.
SORT ITAB BY header1.
LOOP AT ITAB INTO WA.
WA1-HEADER1 = WA-HEADER1.
WA1-HEADER2 = WA-HEADER2.
WA1-HEADER3 = WA-HEADER3.
AT END OF HEADER1.
APPEND WA1 TO ITAB1.
ENDAT.
ENDLOOP.
WRITE: / 'FINAL'.
LOOP AT ITAB1 INTO WA.
WRITE: / WA-header1, WA-header2, wa-header3.
ENDLOOP.
‎2015 Jan 08 6:04 PM
that table is sorted only by key of the header 1 so maybe your result in a future will be not the correct...
init variable actual with the first row of the table
loop.
save the actual value of header1 in a variable actual.
when the next value is different append the preview row to result table..
endloop.
here a link for abap code https://training.sap.com/shop/certification/c_taw12_731-sap-certified-development-associate---abap-w...
regards.
Alberto
‎2015 Jan 08 6:09 PM
well, i can try to search the SAP FM and get a table for each one of those aaaa bb cccc. But then again i need to read the last line. i can get that by sy-tabix right?
and then read the index = sy-tabix.
‎2015 Jan 08 6:59 PM
hi bruno,
Try this code for the solution.
REPORT ZR_TEST2.
TYPES: BEGIN OF TY,
HEADER1 TYPE C LENGTH 10,
HEADER2 TYPE C LENGTH 10,
HEADER3 TYPE C LENGTH 10,
END OF TY.
DATA: WA TYPE TY,
WA1 TYPE TY,
ITAB LIKE STANDARD TABLE OF WA,
ITAB1 LIKE STANDARD TABLE OF WA.
WA-HEADER1 = 'a'.
WA-HEADER2 = '2132'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.
WA-HEADER1 = 'a'.
WA-HEADER2 = '312'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.
WA-HEADER1 = 'a'.
WA-HEADER2 = '312'.
WA-HEADER3 = '312'.
APPEND WA TO ITAB.
CLEAR WA.
WA-HEADER1 = 'b'.
WA-HEADER2 = '132'.
WA-HEADER3 = '132'.
APPEND WA TO ITAB.
CLEAR WA.
WA-HEADER1 = 'c'.
WA-HEADER2 = '32'.
WA-HEADER3 = '123'.
APPEND WA TO ITAB.
CLEAR WA.
WA-HEADER1 = 'c'.
WA-HEADER2 = '12'.
WA-HEADER3 = '13'.
APPEND WA TO ITAB.
CLEAR WA.
SORT ITAB BY header1.
LOOP AT ITAB INTO WA.
WA1-HEADER1 = WA-HEADER1.
WA1-HEADER2 = WA-HEADER2.
WA1-HEADER3 = WA-HEADER3.
AT END OF HEADER1.
APPEND WA1 TO ITAB1.
ENDAT.
ENDLOOP.
WRITE: / 'FINAL'.
LOOP AT ITAB1 INTO WA.
WRITE: / WA-header1, WA-header2, wa-header3.
ENDLOOP.
‎2015 Jan 08 8:14 PM
‎2015 Jan 08 9:55 PM
Although this does work in this case, I prefer to take care of things like this manually. See what happens of you sort by header3 and do AT END OF header3 in the loop.
Rob
‎2015 Jan 08 11:12 PM
‎2015 Jan 08 11:37 PM
Interesting, AT END have a isussue, if the column isn't the first of the table not work properly.
the Help Say..
thanks
‎2015 Jan 09 7:50 AM
within the AT...... ENDAT the fields are filled with '*'. therefore before this AT....ENDAT I am assigning values to work area wa1.
AT END ... is used here to get the last record only.
‎2015 Jan 09 2:22 PM
abdul raheem mohammad wrote:
within the AT...... ENDAT the fields are filled with '*'. therefore before this AT....ENDAT I am assigning values to work area wa1.
AT END ... is used here to get the last record only.
And that's the problem. Since the dawn of time, programmers have written code to do a specific task. We need to develop code that is more easily reusable.
After all, the dirty little secret of programmers is that a large portion of what we do is <CTL>C <CTL>V.
Rob
‎2015 Jan 09 3:04 PM
Any sugestion aboup a better way to do it?
Is better to check the lines of the itab with sy-tabix and then read the last one by it's index? is it possible to do it with an internal table? (it should have a header line)
sorrry the questions.
i'm starting in this whole new world of abap! all the best.
‎2015 Jan 09 3:16 PM
Like I said, I prefer to do it manually. Keep a work are with the last value of the field you are interested in. Compare the next value with the old value. When it changes, do what you need to do and replace the work area with the new value.
I'm sure there are better ways. This just what I have done since running into the noted problems with AT NEW, ON CHANGE OF, etc.
Rob