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

Fill internal table based on two columns

Former Member
0 Likes
2,298

Hi ABAPers!

I have the sequent request:

from an int table with two columns "KEY" and "VALUE" I have to create another one like this:

as is:

KEY |VALUE |

ID |0001 |

NAME |bob |

SURNAME|smith |

ID |0002 |

NAME |jack |

SURNAME|black |

to be:

ID |NAME |SURNAME |

0001 |bob |smith |

0002 |jack |black |

Can someone help me?

Thanks

Vil

1 ACCEPTED SOLUTION
Read only

MateuszAdamus
Active Contributor
2,210

Hello avilmercati

TYPES:
  BEGIN OF source,
    key TYPE string,
    value TYPE string,
  END OF source,

  BEGIN OF target,
    id TYPE string,
    name TYPE string, 
    surname TYPE string,
  END OF target.

DATA:
  lv_field_count TYPE i,
  ls_target TYPE target,
  lt_target TYPE TABLE OF target.

FIELD-SYMBOLS:
  <lv_field> TYPE any.

CLEAR ls_target.
LOOP AT lt_source REFERENCE INTO DATA(ld_source).
  ASSIGN COMPONENT ld_source->key OF STRUCTURE ls_target TO <lv_field>.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

  <lv_field> = ld_source->value.
  lv_field_count = lv_field_count + 1.

  IF lv_field_count = 3.
    INSERT ls_target INTO TABLE lt_target.

    CLEAR:
      lv_field_count.
      ls_target.
  ENDIF.
ENDLOOP.

Kind regards,
Mateusz

Hello avilmercati

TYPES:
  BEGIN OF source,
    key TYPE string,
    value TYPE string,
  END OF source,

  BEGIN OF target,
    id TYPE string,
    name TYPE string, 
    surname TYPE string,
  END OF target.

DATA:
  lv_field_count TYPE i,
  ls_target TYPE target,
  lt_target TYPE TABLE OF target.

FIELD-SYMBOLS:
  <lv_field> TYPE any.

CLEAR ls_target.
LOOP AT lt_source REFERENCE INTO DATA(ld_source).
  ASSIGN COMPONENT ld_source->key OF STRUCTURE ls_target TO <lv_field>.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

  <lv_field> = ld_source->value.
  lv_field_count = lv_field_count + 1.

  IF lv_field_count = 3.
    INSERT ls_target INTO TABLE lt_target.

    CLEAR:
      lv_field_count.
      ls_target.
  ENDIF.
ENDLOOP.

Kind regards,
Mateusz

5 REPLIES 5
Read only

lenastodal
Product and Topic Expert
Product and Topic Expert
0 Likes
2,210

Thank you for visiting SAP Community to get answers to your questions. Since this is your first question, I recommend that you familiarize yourself with our Q&A Tutorial: https://developers.sap.com/tutorials/community-qa.html, as it provides tips for preparing questions that draw responses from our members.
Should you wish, you can revise your question by selecting Actions, then Edit.

By adding a picture to your profile you encourage readers to respond:https://www.youtube.com/watch?v=46bt1juWUUM
Many thanks!


Join or subscribe to SAP Community Groups to stay up-to-date, including SAP TechEd Group.
Read only

MateuszAdamus
Active Contributor
2,211

Hello avilmercati

TYPES:
  BEGIN OF source,
    key TYPE string,
    value TYPE string,
  END OF source,

  BEGIN OF target,
    id TYPE string,
    name TYPE string, 
    surname TYPE string,
  END OF target.

DATA:
  lv_field_count TYPE i,
  ls_target TYPE target,
  lt_target TYPE TABLE OF target.

FIELD-SYMBOLS:
  <lv_field> TYPE any.

CLEAR ls_target.
LOOP AT lt_source REFERENCE INTO DATA(ld_source).
  ASSIGN COMPONENT ld_source->key OF STRUCTURE ls_target TO <lv_field>.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

  <lv_field> = ld_source->value.
  lv_field_count = lv_field_count + 1.

  IF lv_field_count = 3.
    INSERT ls_target INTO TABLE lt_target.

    CLEAR:
      lv_field_count.
      ls_target.
  ENDIF.
ENDLOOP.

Kind regards,
Mateusz

Read only

2,210

Nice code Mateusz!
It works fine!

Thanks!

Read only

0 Likes
2,210

Use FOR ALL ENTRIES

Read only

Sandra_Rossi
Active Contributor
2,210

Please use formatting options (CODE button).

Example:

as is:

KEY    |VALUE |
ID |0001 |
NAME |bob |
SURNAME|smith |
ID |0002 |
NAME |jack |
SURNAME|black |

to be:

ID   |NAME |SURNAME |
0001 |bob |smith |
0002 |jack |black |