ABAP 7.5 Syntax - FOR Statement - Examples 3 and 4
What does the below Report do?
There is an internal table with two fields. We need to capture the contents of more than one field of the internal table into another internal table with a condition. What will be done usually? We will use LOOP statement with WHERE clause and append data for every row in the main internal table. Now we don't have to do that because of the introduction of the powerful FOR statement.
Report Example 3:
*&---------------------------------------------------------------------*
*& Report ZAG_TEST_FOR_TWO
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zag_test_for_two.
*-> Local Data Declarations
TYPES: BEGIN OF lty_parent,
no1 TYPE i,
no2 TYPE i,
no3 TYPE i,
END OF lty_parent,
BEGIN OF lty_child,
field1 TYPE i,
field2 TYPE i,
END OF lty_child,
ltt_parent TYPE STANDARD TABLE OF lty_parent WITH EMPTY KEY,
ltt_child TYPE STANDARD TABLE OF lty_child WITH EMPTY KEY.
DATA(lt_parent) = VALUE ltt_parent( ( no1 = 1 no2 = 2 no3 = 3 )
( no1 = 2 no2 = 3 no3 = 2 )
( no1 = 3 no2 = 4 no3 = 1 )
( no1 = 4 no2 = 5 no3 = 8 )
( no1 = 5 no2 = 6 no3 = 9 ) ).
WRITE 'Parent table data:'.
LOOP AT lt_parent ASSIGNING FIELD-SYMBOL(<lfs_parent>).
WRITE: / <lfs_parent>-no1, <lfs_parent>-no2, <lfs_parent>-no3.
ENDLOOP.
SKIP.
WRITE / 'Child data:'.
DATA(lt_child) = VALUE ltt_child( FOR <lfs_par> IN lt_parent
WHERE ( no2 = 5 )
( field1 = <lfs_par>-no2
field2 = <lfs_par>-no3 ) ).
LOOP AT lt_child ASSIGNING FIELD-SYMBOL(<lfs_child>).
WRITE: / <lfs_child>-field1, <lfs_child>-field2.
ENDLOOP.
Output:
Parent table data:
1 2 3
2 3 2
3 4 1
4 5 8
5 6 9
Child data:
5 8
Example 4:
Use of UNTIL and WHILE in FOR syntax:
Report Example:
*&---------------------------------------------------------------------*
*& Report ZAG_TEST_FOR_TWO
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zag_test_for_three.
*-> Local Data Declarations
TYPES: BEGIN OF lty_parent,
field1 TYPE i,
field2 TYPE i,
END OF lty_parent,
ltt_parent TYPE STANDARD TABLE OF lty_parent WITH EMPTY KEY.
*-> Process
WRITE / 'FOR Statement - THEN and UNTIL'.
DATA(lt_parent) = VALUE ltt_parent( FOR i = 0 THEN i + 1 UNTIL i > 4 "Similar to C++ FOR but condition is reverse
( field1 = i
field2 = sy-index ) ).
LOOP AT lt_parent ASSIGNING FIELD-SYMBOL(<lfs_parent>).
WRITE: / <lfs_parent>-field1, <lfs_parent>-field2.
ENDLOOP.
CLEAR lt_parent.
SKIP.
WRITE / 'FOR Statement - THEN and WHILE'.
lt_parent = VALUE ltt_parent( FOR i = 0 THEN i + 1 WHILE i < 4 "Exactly similar to C++ FOR
( field1 = i
field2 = sy-tabix ) ).
LOOP AT lt_parent ASSIGNING <lfs_parent>.
WRITE: / <lfs_parent>-field1, <lfs_parent>-field2.
ENDLOOP.
Note: sy-index and sy-tabix has no effect on FOR statements.
FOR Statement - THEN and UNTIL
0 0
1 0
2 0
3 0
4 0
FOR Statement - THEN and WHILE
0 1
1 1
2 1
3 1
There is an internal table with two fields. We need to capture the contents of more than one field of the internal table into another internal table with a condition. What will be done usually? We will use LOOP statement with WHERE clause and append data for every row in the main internal table. Now we don't have to do that because of the introduction of the powerful FOR statement.
Report Example 3:
*&---------------------------------------------------------------------*
*& Report ZAG_TEST_FOR_TWO
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zag_test_for_two.
*-> Local Data Declarations
TYPES: BEGIN OF lty_parent,
no1 TYPE i,
no2 TYPE i,
no3 TYPE i,
END OF lty_parent,
BEGIN OF lty_child,
field1 TYPE i,
field2 TYPE i,
END OF lty_child,
ltt_parent TYPE STANDARD TABLE OF lty_parent WITH EMPTY KEY,
ltt_child TYPE STANDARD TABLE OF lty_child WITH EMPTY KEY.
DATA(lt_parent) = VALUE ltt_parent( ( no1 = 1 no2 = 2 no3 = 3 )
( no1 = 2 no2 = 3 no3 = 2 )
( no1 = 3 no2 = 4 no3 = 1 )
( no1 = 4 no2 = 5 no3 = 8 )
( no1 = 5 no2 = 6 no3 = 9 ) ).
WRITE 'Parent table data:'.
LOOP AT lt_parent ASSIGNING FIELD-SYMBOL(<lfs_parent>).
WRITE: / <lfs_parent>-no1, <lfs_parent>-no2, <lfs_parent>-no3.
ENDLOOP.
SKIP.
WRITE / 'Child data:'.
DATA(lt_child) = VALUE ltt_child( FOR <lfs_par> IN lt_parent
WHERE ( no2 = 5 )
( field1 = <lfs_par>-no2
field2 = <lfs_par>-no3 ) ).
LOOP AT lt_child ASSIGNING FIELD-SYMBOL(<lfs_child>).
WRITE: / <lfs_child>-field1, <lfs_child>-field2.
ENDLOOP.
Output:
Parent table data:
1 2 3
2 3 2
3 4 1
4 5 8
5 6 9
Child data:
5 8
Example 4:
Use of UNTIL and WHILE in FOR syntax:
Report Example:
*&---------------------------------------------------------------------*
*& Report ZAG_TEST_FOR_TWO
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zag_test_for_three.
*-> Local Data Declarations
TYPES: BEGIN OF lty_parent,
field1 TYPE i,
field2 TYPE i,
END OF lty_parent,
ltt_parent TYPE STANDARD TABLE OF lty_parent WITH EMPTY KEY.
*-> Process
WRITE / 'FOR Statement - THEN and UNTIL'.
DATA(lt_parent) = VALUE ltt_parent( FOR i = 0 THEN i + 1 UNTIL i > 4 "Similar to C++ FOR but condition is reverse
( field1 = i
field2 = sy-index ) ).
LOOP AT lt_parent ASSIGNING FIELD-SYMBOL(<lfs_parent>).
WRITE: / <lfs_parent>-field1, <lfs_parent>-field2.
ENDLOOP.
CLEAR lt_parent.
SKIP.
WRITE / 'FOR Statement - THEN and WHILE'.
lt_parent = VALUE ltt_parent( FOR i = 0 THEN i + 1 WHILE i < 4 "Exactly similar to C++ FOR
( field1 = i
field2 = sy-tabix ) ).
LOOP AT lt_parent ASSIGNING <lfs_parent>.
WRITE: / <lfs_parent>-field1, <lfs_parent>-field2.
ENDLOOP.
Note: sy-index and sy-tabix has no effect on FOR statements.
FOR Statement - THEN and UNTIL
0 0
1 0
2 0
3 0
4 0
FOR Statement - THEN and WHILE
0 1
1 1
2 1
3 1
Comments
Post a Comment