2020 Apr 07 8:52 AM
Hello together,
is it possible to write this in new syntax by using a for loop?
LOOP AT mt_xyz assigning <ls_xyz>.
if <ls_xyz> <> 'C'.
CONTINUE.
endif.
READ TABLE lt_abc assigning <ls_abc> with key parent_key components parent_key = <ls_xyz>-key.
if sy-subrc <> 0.
CONTINUE.
endif.
if <ls_xyz>-field = 'z'.
ls_key-key = <ls_xyz>-key.
INSERT ls_key into table lt_keys.
endif.
ENDLOOP.
Kind regards,
Pascal
2020 Apr 07 9:39 AM
This is the statement that should work for you, after you have probably modified the TABLE TYPE definitions (and probably other corrections, such as the attribute restriction on MT_XYZ <> C, your coding is missing the attribute)
DATA(lt_keys) = VALUE tty_keys( FOR wa1 IN mt_xyz WHERE ( key <> 'C' AND field = 'z' )
FOR wa2 IN lt_abc USING KEY parent_key
WHERE ( parent_key = wa1-key )
( key = wa1-key ) ).
Here is a test report with sample data and the output for the above coding:
REPORT ZVALUESFOREXAMPLE.
TYPES: BEGIN OF ty_key,
key TYPE char1,
END OF ty_key,
tty_keys TYPE STANDARD TABLE OF ty_key WITH EMPTY KEY,
BEGIN OF ty_xyz,
key TYPE char1,
field TYPE char1,
END OF ty_xyz,
tty_xyz TYPE STANDARD TABLE OF ty_xyz WITH EMPTY KEY,
BEGIN OF ty_abc,
key TYPE char1,
parent_key TYPE char1,
END OF ty_abc,
tty_abc TYPE STANDARD TABLE OF ty_abc WITH EMPTY KEY
WITH NON-UNIQUE SORTED KEY parent_key COMPONENTS parent_key.
" Test data for table mt_xyz
DATA(mt_xyz) = VALUE tty_xyz( ( key = 'A' field = 'z' )
( key = 'B' field = 'y' )
( key = 'C' field = 'x' )
( key = 'D' field = 'w' ) ).
" Test data for table lt_abc
DATA(lt_abc) = VALUE tty_abc( ( key = 'B' parent_key = 'A' )
( key = 'D' parent_key = 'C' ) ).
" THIS IS THE ACTUAL STATEMENT YOU ARE LOOKING FOR
" Creation of values for table lt_keys based on mt_xyz and lt_abc
DATA(lt_keys) = VALUE tty_keys( FOR wa1 IN mt_xyz WHERE ( key <> 'C' AND field = 'z' )
FOR wa2 IN lt_abc USING KEY parent_key
WHERE ( parent_key = wa1-key )
( key = wa1-key ) ).
" Output of example data
DATA(out) = cl_demo_output=>new( ).
out->write( mt_xyz ).
out->write( lt_abc ).
out->write( lt_keys ).
out->display( ).
2020 Apr 07 9:39 AM
This is the statement that should work for you, after you have probably modified the TABLE TYPE definitions (and probably other corrections, such as the attribute restriction on MT_XYZ <> C, your coding is missing the attribute)
DATA(lt_keys) = VALUE tty_keys( FOR wa1 IN mt_xyz WHERE ( key <> 'C' AND field = 'z' )
FOR wa2 IN lt_abc USING KEY parent_key
WHERE ( parent_key = wa1-key )
( key = wa1-key ) ).
Here is a test report with sample data and the output for the above coding:
REPORT ZVALUESFOREXAMPLE.
TYPES: BEGIN OF ty_key,
key TYPE char1,
END OF ty_key,
tty_keys TYPE STANDARD TABLE OF ty_key WITH EMPTY KEY,
BEGIN OF ty_xyz,
key TYPE char1,
field TYPE char1,
END OF ty_xyz,
tty_xyz TYPE STANDARD TABLE OF ty_xyz WITH EMPTY KEY,
BEGIN OF ty_abc,
key TYPE char1,
parent_key TYPE char1,
END OF ty_abc,
tty_abc TYPE STANDARD TABLE OF ty_abc WITH EMPTY KEY
WITH NON-UNIQUE SORTED KEY parent_key COMPONENTS parent_key.
" Test data for table mt_xyz
DATA(mt_xyz) = VALUE tty_xyz( ( key = 'A' field = 'z' )
( key = 'B' field = 'y' )
( key = 'C' field = 'x' )
( key = 'D' field = 'w' ) ).
" Test data for table lt_abc
DATA(lt_abc) = VALUE tty_abc( ( key = 'B' parent_key = 'A' )
( key = 'D' parent_key = 'C' ) ).
" THIS IS THE ACTUAL STATEMENT YOU ARE LOOKING FOR
" Creation of values for table lt_keys based on mt_xyz and lt_abc
DATA(lt_keys) = VALUE tty_keys( FOR wa1 IN mt_xyz WHERE ( key <> 'C' AND field = 'z' )
FOR wa2 IN lt_abc USING KEY parent_key
WHERE ( parent_key = wa1-key )
( key = wa1-key ) ).
" Output of example data
DATA(out) = cl_demo_output=>new( ).
out->write( mt_xyz ).
out->write( lt_abc ).
out->write( lt_keys ).
out->display( ).
2020 Apr 07 9:46 AM
Hello,
thanks for your fast reply.
How to add CONTINUE and the Read Table statement?
BR,
Pascal
2020 Apr 07 9:53 AM
coding1407, the above coding is the final construct with the new syntax of what you did with the old syntax.
Try it out and play with it.