FOR
This can be used in place of normal LOOP statement that we had in ECC.
It is more related to what we have seen in JAVA or C .Hence it can be easily understood by someone who is new to ABAP world.
Note that we can't debug inside the FOR loop and also sy-index and sy-tabix values will not be filled inside FOR loop
FOR provides us with many features in addition to simple loop so now lets see all those features one by one with an example .
TYPES : BEGIN OF ty_target,
name TYPE char10,
value TYPE char20,
exp TYPE i,
END OF ty_target,
tt_target TYPE STANDARD TABLE OF ty_target WITH DEFAULT KEY,
BEGIN OF ty_source,
name TYPE char10,
value TYPE char20,
END OF ty_source,
tt_source TYPE TABLE OF ty_source WITH DEFAULT KEY.
DATA(lt_source) = VALUE tt_source(
( name = 'TECH' value = 'SAP' )
( name = 'TECH' value = 'SAP1' )
( name = 'TECH' value = 'SAP3' )
( name = 'DOMAIN' value = 'ABAP' )
( name = 'DOMAIN1' value = 'ABAP1' )
( name = 'DOMAIN' value = 'ABAP3' )
).
3.6.1. Simple FOR loop to iterate one table and fill data in another table from
DATA(lt_target) = VALUE tt_target( FOR ls_source IN lt_source
(
name = ls_source-name
value = ls_source-value
)
).
" Note that ls_source will get temporarily created in the loop
3.6.2. Exchanging values
lt_target = VALUE #( FOR ls_source IN lt_source
(
name = ls_source-value
value = ls_source-name
)
).
3.6.3. Using where clause
lt_target = VALUE #( FOR ls_source IN lt_source
WHERE ( name = 'TECH' OR name = 'DOMAIN' )
(
name = ls_source-name
value = ls_source-value
)
).
3.6.4. Using FOR with LOOP and GROUP BY
* Lets use the example which we have created for LOOP and GROUP BY
lt_target = VALUE #(
( name = 'TECH2' value = 'SAP' exp = '5' )
( name = 'DOMAIN2' value = 'ABAP2' exp = '10')
( name = 'TECH' value = 'SAP' exp = '20')
( name = 'TECH3' value = 'SAP' exp = '15' )
( name = 'DOMAIN3' value = 'ABAP3' exp = '10')
).
LOOP AT lt_target ASSIGNING FIELD-SYMBOL(<fs1>)
GROUP BY ( value = <fs1>-value
size = GROUP SIZE " Defines the number of rows in a group
index = GROUP INDEX " Index of group
)
ASCENDING "Optional - by default its ascending , groups can be sorted
ASSIGNING FIELD-SYMBOL(<fs_grp>).
DATA(lt_group_data) = VALUE tt_target(
FOR <fs2> IN GROUP <fs_grp> ( <fs2> )
).
cl_demo_output=>write( lt_group_data ).
CLEAR lt_group_data.
ENDLOOP.
WRITE / 'Created groups data'.
cl_demo_output=>display( lt_group_data ).
Output for all three groups :
Group1. VALUE = ‘ABAP2’
Group2. VALUE = ‘ABAP3’
Group3. VALUE = ‘SAP’
3.6.5. Use of LET with FOR loop
TYPES : BEGIN OF ty_int1,
value TYPE i,
value1 TYPE i,
value2 TYPE i,
END OF ty_int1,
tt_int1 TYPE STANDARD TABLE OF ty_int1 WITH DEFAULT KEY.
DATA(lt_int1) = VALUE tt_int1(
( value = '10' value1 = '20' )
( value = '30' value1 = '40' )
).
Created table :
DATA(lt_int_changed) = VALUE tt_int1( FOR ls_int1 IN lt_int1
LET lv1_for = ls_int1-value + 10
lv2_for = lv1_for + 20
IN value = lv1_for
value1 = lv2_for
3.6.6. Using WHILE and UNTIL with FOR :
We will using using structure created for above example itself.
lt_target = VALUE #( FOR lv_1 = 20 THEN lv_1 - 2 WHILE lv_1 GE 10
( name = lv_1
value = lv_1 + 10
exp = lv_1 + 20
)
).
Note : In above code lines 'lv_1' in created temporarily inside FOR loop.
3.6.7. Using UNTIL : It is like opposite to WHILE
lt_target = VALUE #( FOR lv_1 = 1 THEN lv_1 + 2 UNTIL lv_1 GE 10
( name = lv_1
value = lv_1 + 10
exp = lv_1 + 20
)
).
3.6.8. FOR : involving multiple tables
TYPES : BEGIN OF ty_temp,
name TYPE char10,
company TYPE char20,
END OF ty_temp,
tt_temp TYPE STANDARD TABLE OF ty_temp WITH DEFAULT KEY,
BEGIN OF ty_final,
name TYPE char10,
value TYPE char20,
exp TYPE i,
company TYPE char20,
END OF ty_final,
tt_final TYPE STANDARD TABLE OF ty_final WITH DEFAULT KEY.
DATA(lt_temp) = VALUE tt_temp(
( name = 'TECH2' company = 'SAP' )
( name = 'TECH1' company = 'AMAZON' )
( name = 'DOMAIN2' company = 'GOOGLE')
( name = 'TECH' company = 'APPLE' )
( name = 'DOMAIN1' company = 'FLIPKART')
( name = 'TECH3' company = 'MICROSOFT' )
( name = 'DOMAIN3' company = 'FACEBOOK' )
).
lt_target = VALUE #(
( name = 'TECH' value = 'SAP' exp = '20')
( name = 'TECH2' value = 'SAP2' exp = '5' )
( name = 'DOMAIN2' value = 'ABAP2' exp = '10')
( name = 'TECH1' value = 'SAP1' exp = '20')
( name = 'TECH3' value = 'SAP3' exp = '15' )
( name = 'DOMAIN3' value = 'ABAP3' exp = '10')
).
lt_source = VALUE #(
( name = 'TECH' value = 'SAP' )
( name = 'TECH1' value = 'SAP1' )
( name = 'TECH2' value = 'SAP2' )
( name = 'TECH2' value = 'SAP2REP' )
( name = 'DOMAIN' value = 'ABAP' )
( name = 'DOMAIN1' value = 'ABAP1' )
( name = 'DOMAIN2' value = 'ABAP2' )
( name = 'DOMAIN3' value = 'ABAP3' )
).
Above created tables :
DATA(lt_final) = NEW tt_final( FOR ls_temp IN lt_temp
FOR ls_target IN lt_target
WHERE ( name = ls_temp-name )
FOR ls_source IN lt_source
WHERE ( name = ls_target-name )
( name = ls_source-name
value = ls_source-value
exp = ls_target-exp + sy-index
company = ls_temp-company
)
).
LT_FINAL internal table is created as an object reference :
Next - Loop with REDUCE
No comments:
Post a Comment