ABAP 7.5 Syntax - FOR Statement - Example 1
What does the below Report do?
There is an internal table with two fields. We need to capture the contents of one of the fields of the internal table into another internal table. What will be done usually? We will use LOOP statement 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 1:
*&---------------------------------------------------------------------*
*& Report ZAG_TEST_FOR_ONE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zag_test_for_one.
*-> Local Data Declarations
TYPES: BEGIN OF lty_parent,
no1 TYPE i,
no2 TYPE i,
END OF lty_parent,
ltt_parent TYPE STANDARD TABLE OF lty_parent WITH EMPTY KEY,
ltt_child TYPE STANDARD TABLE OF i WITH EMPTY KEY.
DATA(lt_parent) = VALUE ltt_parent( ( no1 = 1 no2 = 2 )
( no1 = 3 no2 = 4 )
( no1 = 5 no2 = 6 ) ).
WRITE / 'Print Parent data'.
LOOP AT lt_parent ASSIGNING FIELD-SYMBOL(<lfs_parent>).
WRITE: / <lfs_parent>-no1, <lfs_parent>-no2.
ENDLOOP.
SKIP.
WRITE / 'Print Child data'.
DATA(lt_child) = VALUE ltt_child( FOR <lfs_child> IN lt_parent
( <lfs_child>-no2 ) ).
LOOP AT lt_child ASSIGNING FIELD-SYMBOL(<lfs_child_op>).
WRITE / <lfs_child_op>.
ENDLOOP.
Output:
Print Parent data
1 2
3 4
5 6
Print Child data
2
4
6
There is an internal table with two fields. We need to capture the contents of one of the fields of the internal table into another internal table. What will be done usually? We will use LOOP statement 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 1:
*&---------------------------------------------------------------------*
*& Report ZAG_TEST_FOR_ONE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zag_test_for_one.
*-> Local Data Declarations
TYPES: BEGIN OF lty_parent,
no1 TYPE i,
no2 TYPE i,
END OF lty_parent,
ltt_parent TYPE STANDARD TABLE OF lty_parent WITH EMPTY KEY,
ltt_child TYPE STANDARD TABLE OF i WITH EMPTY KEY.
DATA(lt_parent) = VALUE ltt_parent( ( no1 = 1 no2 = 2 )
( no1 = 3 no2 = 4 )
( no1 = 5 no2 = 6 ) ).
WRITE / 'Print Parent data'.
LOOP AT lt_parent ASSIGNING FIELD-SYMBOL(<lfs_parent>).
WRITE: / <lfs_parent>-no1, <lfs_parent>-no2.
ENDLOOP.
SKIP.
WRITE / 'Print Child data'.
DATA(lt_child) = VALUE ltt_child( FOR <lfs_child> IN lt_parent
( <lfs_child>-no2 ) ).
LOOP AT lt_child ASSIGNING FIELD-SYMBOL(<lfs_child_op>).
WRITE / <lfs_child_op>.
ENDLOOP.
Output:
Print Parent data
1 2
3 4
5 6
Print Child data
2
4
6
Comments
Post a Comment