Skip to Content

ABAP new Syntax - Conditional FOR Loop with insert etc.

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

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Best Answer
    Posted on Apr 07 at 08: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( ).
    


    Add a comment
    10|10000 characters needed characters exceeded

    • Pascal Ditzel, 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.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.