Application Development 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: 

FOR expressions with CORRESPONDING operator and changing values

MichiFr
Participant
0 Kudos

Hi,


I often have the requirement to copy an internal table into another and add/change some of the target's table members. E.g.:


REPORT zsdsm_mf_test_corresponding.
INCLUDE <icons>.

TYPES t_bapicond TYPE bapicond.

TYPES:
BEGIN OF t_bapicond2.
TYPES: icon TYPE icon_d.
        INCLUDE TYPE bapicond.
TYPES: END OF t_bapicond2.


DATA:
  lt_source   TYPE STANDARD TABLE OF t_bapicond
  ,lt_target  TYPE STANDARD TABLE OF t_bapicond2
  .

START-OF-SELECTION.
  BREAK-POINT.

  "Sample Data
  lt_source = VALUE #(
      LET o_rnd = cl_abap_random_int=>create(
        seed = CONV i( sy-uzeit ) min = 1 max = 599 ) IN
      ( itm_number = lines( lt_source ) * 10
        applicatio = 'V' cond_value = o_rnd->get_next( ) )
      ( itm_number = lines( lt_source ) * 10
        applicatio = 'V' cond_value = o_rnd->get_next( ) condisacti = abap_true )
      ( itm_number = lines( lt_source ) * 10
        applicatio = 'V' cond_value = o_rnd->get_next( ) condisacti = abap_true )
      ( itm_number = lines( lt_source ) * 10
        applicatio = 'V' cond_value = o_rnd->get_next( ) )
      ( itm_number = lines( lt_source ) * 10
        applicatio = 'V' cond_value = o_rnd->get_next( ) )
  ).

  "OK
  lt_target = CORRESPONDING #( lt_source ).

  "OK
  lt_target = VALUE #(
   FOR wa IN lt_source (
      CORRESPONDING #( wa )
      )
   ).

  "All inactive conditions should get a yellow icon! <<<

  "This works pretty well usign  "old" coding...
  CLEAR: lt_target[].
  LOOP AT lt_source ASSIGNING FIELD-SYMBOL(<fs_src>).
    APPEND CORRESPONDING #( <fs_src> ) TO lt_target
      REFERENCE INTO DATA(ref_tgt).
    ref_tgt->icon = COND #(
      WHEN <fs_src>-condisacti = abap_true
        THEN icon_led_yellow
      ELSE icon_space ).
  ENDLOOP.

  "How to do this way? Using FOR expression and CORRESPONDING
*  lt_target = VALUE #(
*   FOR wa IN lt_source (
*      CORRESPONDING #( wa )
*      ICON = COND icon_d( WHEN wa-condisacti = abap_true THEN icon_led_yellow ELSE icon_space )
*      )
*   ).
  BREAK-POINT.

Starting from line 54 I tried to replace the coding from lines 44 up to 51, but I had no luck so far.

Any hints?

Thanks,
Michael

1 ACCEPTED SOLUTION

Former Member

After some trying around, I got it!

  lt_target = VALUE #(

   FOR wa IN lt_source

    ( CORRESPONDING #(

        BASE ( value #( icon = COND icon_d( WHEN wa-condisacti = abap_true

                                            THEN icon_led_yellow

                                            ELSE icon_space ) ) )

        wa ) )

   ).

Message was edited by: Armin Junge Formatting

8 REPLIES 8

raphael_almeida
Active Contributor
0 Kudos

Hi Michael,

Something like that:

    DATA(new) = REDUCE alv_t( INIT out = VALUE alv_t( )
                                               FOR i IN itab
                                               NEXT out = COND alv_t(
                                               WHEN i-status = 'A'
                                               THEN VALUE #( BASE out ( VALUE alv_y( BASE CORRESPONDING alv_y( i )
                                                                                                     icon = c_icon_green ) ) )
                                               WHEN i-status = 'R'
                                               THEN VALUE #( BASE out ( VALUE alv_y( BASE CORRESPONDING alv_y( i )
                                                                                                     icon = c_icon_red ) ) )

                                               ELSE VALUE #( BASE out ( VALUE alv_y( BASE CORRESPONDING alv_y( i )
                                                                                                     icon = c_icon_off ) ) ) ) ).


Where: alv_t is TYPE STANDARD of alv_y and the itab is internal table with values.


Regards,


Raphael Pacheco.



Former Member

After some trying around, I got it!

  lt_target = VALUE #(

   FOR wa IN lt_source

    ( CORRESPONDING #(

        BASE ( value #( icon = COND icon_d( WHEN wa-condisacti = abap_true

                                            THEN icon_led_yellow

                                            ELSE icon_space ) ) )

        wa ) )

   ).

Message was edited by: Armin Junge Formatting

0 Kudos

Super

Generally i add in-line comments when using a complex expression like this, so that the developer who's gonna maintain this code knows what is happening

0 Kudos

How about a small ABAP Unit test with 1 positive and 1 negative condition ?

0 Kudos

Me too, but this is the task of the questioner

0 Kudos

Naimesh Patel wrote:

How about a small ABAP Unit test with 1 positive and 1 negative condition ?

What would the AUnit test - the FOR function?

If the method is modular enough (e.g., BUILD_ALV_OUTPUT_TABLE), then i can check the contents of the table to see how many ICON types i have in the output table.

But how do i test for a negative case? If i wrote the code correctly, then there's no negative testcase.

BR,

Suhas

0 Kudos

Of course, i meant in productive code

MichiFr
Participant
0 Kudos

Thanks Armin for this code - very helpful indeed!