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: 
Read only

Operator CORRESPONDING, additional fields

ralf_wenzel_heuristika
Active Participant
0 Likes
5,390

Hi,

I have a target table, which is wider (has more fields) than the source table. I just want to say: "move-corresponding from source itab to target itab AND fill additional fields of target itab with....."

types:

begin of source,

   dummy type char01,

end of source,

  begin of target,

   dummy type char01,

   date type d,

end of target,

  sourcetab type standard table of source,

  targettab type standard table of target.

  targettab = value #( for <i> in sourcetab ( corresponding #( <i> ) ) ).

Question: How can I fill targettab-date with sy-datum for each record, created by this constructor?

11 REPLIES 11
Read only

SzymonKobalczyk
Explorer
0 Likes
3,344

Hi Ralf,


if I understood your question correctly, you have to loop at your source table, move corresponding fields, fill date and append structure to target table, e.g. something like this:

FIELD-SYMBOLS <source> TYPE source.

DATA: ls_target TYPE target.

LOOP AT sourcetab ASSIGNING <source>.

   MOVE-CORRESPONDING <source> TO ls_target.

   ls_target-date = sy-datum.

   APPEND ls_target TO targettab.

ENDLOOP.

Regards,
Szymon

Read only

0 Likes
3,344

Sorry, my question was:


Question: How can I fill targettab-date with sy-datum for each record, created by this constructor?

I know I can loop, but I don't want to

I just want to use the command I mentioned.

Read only

Former Member
3,344

This works:

targettab = value #( for <i> in sourcetab

                       ( dummy = <i>-dummy

                         date = sy-datum

                       )

                     ).

But might not be as flexible as you'd like. If you have control over your data definitions you could use a construction like:

types: begin of source,

          s0 type char01,

          s1 type char01,

          s2 type char01,

        end of source,

        begin of sourcetarget,

          s0 type char01,

          s2 type char01,

        end of sourcetarget,

        begin of target.

          include type sourcetarget as source.

types:   date type d,

        end of target.

data:

   sourcetab type standard table of source,

   targettab type standard table of target.

  sourcetab = value #( ( s0 = 'A' s1 = 'n' s2 = 't' )

                       ( s0 = 'B' s1 = 'o' s2 = 'b' )

                     ).

  targettab = value #( for <i> in sourcetab

                       ( source = corresponding #( <i> )

                         date = sy-datum

                       )

                     ).

  cl_demo_output=>display( targettab ).


If target contains all fields in source you can forego the sourcetarget structure and also change the corresponding construct to a direct assignment, e.g:


  targettab = value #( for <i> in sourcetab

                       ( source = <i>

                         date = sy-datum

                       )

                     ).

Read only

ChristianGnter
Contributor
3,344

Hi Ralf,

i think the base addition of the corresponding operator can help.

ABAP Keyword Documentation

Something like this:


TYPES:
  BEGIN OF source,
    dummy TYPE char01,
  END OF source,

  BEGIN OF target,
    dummy TYPE char01,
    date  TYPE d,
  END OF target,

  sourcetab TYPE STANDARD TABLE OF source
                 WITH NON-UNIQUE DEFAULT KEY,
  targettab TYPE STANDARD TABLE OF target
                 WITH NON-UNIQUE DEFAULT KEY.

DATA(sourcetab) = VALUE sourcetab( ( dummy = 'X' )
                                   ( dummy = 'Y' )
                                   ( dummy = 'Z' ) ).

DATA(targettab) = VALUE targettab(
                    FOR wa IN sourcetab (
                           CORRESPONDING #(
                             " important stuff here:
                             BASE ( VALUE #( date = sy-datum ) )
                             wa ) ) ).

cl_demo_output=>display( targettab ).


Result:



Regards Christian

Read only

0 Likes
3,344

Result: Write-only Coding unless commented appropriately...

Read only

0 Likes
3,344

Volker Wegert wrote:

Result: Write-only Coding unless commented appropriately...

I still can not believe, that appropriate comments are only used for complex commands. Every single untrivial command has to be commented! In particular, a developer shouldn't explain WHAT he es doing, but WHY he is doing that.

Read only

0 Likes
3,344

yes, or as i prefer modularized properly with meaningful method names...

Read only

0 Likes
3,344

The new operators enable us to write very "dense" coding - more "logic" with less code. Since the amount of comments should be a function of the actual logic involved, the new operators require proportionally more comments per instruction than the legacy workarounds.

Note that I wrote "require" - I totally agree on the WHY part, but it doesn't hurt to put the WHAT in plain english as well:

" transfer foo to bar and fill baz with default values BECAUSE X

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
3,344

... Words of wisdom, these! I will keep these points in mind

Read only

0 Likes
3,344

Interesting, useful, and not valid on our 740 system on kernel SP level 7...

Read only

0 Likes
3,344

The base addition was introduced with 7.40 SP08

ABAP Documentation