Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

ABAP new Syntax - Conditional FOR Loop with insert etc.

0 Kudos
9,990

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

1 ACCEPTED SOLUTION

michael_piesche
Active Contributor
7,235

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( ).

3 REPLIES 3

michael_piesche
Active Contributor
7,236

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( ).

0 Kudos
7,235

Hello,

thanks for your fast reply.

How to add CONTINUE and the Read Table statement?

BR,

Pascal

7,235

coding1407, the above coding is the final construct with the new syntax of what you did with the old syntax.

  • The first where condition partially represents the CONTINUE logic.
  • The second where condition represents the Read Table statement.
  • The using key statement makes sure to use a specific key when accessing table values of the second table.
  • The last parenthesized statement creates the new values for the third table.

Try it out and play with it.