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: 

Dynamically convert string to internal table

Former Member
0 Kudos

Hi all,

I want to convert a string to an internal table. There are 2 questions I would like to ask:

1. I have created a FM to fulfill my requirement. The codes are as below:


FUNCTION ZCONVERT_STRING_TO_TABLE.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(INPUT_STRING) TYPE  STRING
*"     REFERENCE(DELIMITER) TYPE  CHAR1 DEFAULT ','
*"     REFERENCE(WITH_HEADER) TYPE  CHAR1 DEFAULT 'X'
*"  TABLES
*"      OUTPUT_TABLE
*"----------------------------------------------------------------------

  TYPES:
    BEGIN OF GTY_OUTPUT,
      BUKRS TYPE BUKRS,
      TXT50 TYPE TXA50_ANLT,
    END OF GTY_OUTPUT.

  DATA:
    GW_INPUT TYPE STRING,
    GS_RESULT TYPE MATCH_RESULT,
    GW_BEGIN_POS TYPE I,
    GT_DATA TYPE STANDARD TABLE OF STRING,
    GS_DATA TYPE STRING,
    GW_BUKRS TYPE BUKRS,
    GW_TXT50 TYPE TXA50_ANLT,
    GS_OUTPUT TYPE GTY_OUTPUT.

* Data initialization
  GW_INPUT = INPUT_STRING.

* Remove prefix if exists
  FIND FIRST OCCURRENCE OF '>' IN GW_INPUT RESULTS GS_RESULT.
  GW_BEGIN_POS = GS_RESULT-OFFSET + 1.
  SHIFT GW_INPUT BY GW_BEGIN_POS PLACES.

* Remove header line
  IF WITH_HEADER = 'X'.
    FIND FIRST OCCURRENCE OF CL_ABAP_CHAR_UTILITIES=>NEWLINE IN GW_INPUT RESULTS GS_RESULT.
    GW_BEGIN_POS = GS_RESULT-OFFSET + 1.
    SHIFT GW_INPUT BY GW_BEGIN_POS PLACES.

  ENDIF.

* Import line items into table
  SPLIT GW_INPUT AT CL_ABAP_CHAR_UTILITIES=>NEWLINE INTO TABLE GT_DATA.

* Parse line items and import to output table
  LOOP AT GT_DATA INTO GS_DATA.
    SPLIT GS_DATA AT DELIMITER INTO GW_BUKRS GW_TXT50.
    GS_OUTPUT-BUKRS = GW_BUKRS.
    GS_OUTPUT-TXT50 = GW_TXT50.
    APPEND GS_OUTPUT TO OUTPUT_TABLE.

  ENDLOOP.

ENDFUNCTION.

However my FM can only convert a string to an internal table that has fixed type, that is the string's content also matches with table type.

Could you help me making the FM to be able to apply for every string (the table type dynamically matches with the string's content)?

2. In an easier way, could you tell me what existing FM can do this?

Thanks in advance

Nguyen

1 ACCEPTED SOLUTION

Former Member
0 Kudos

THere is a way.

Call method CREATE_DYNAMIC_TABLE of class CL_ALV_TABLE_CREATE.

All you need to do is build a field catalog as you build to a ALV display and you will get a reference of the interntal table as an export parameter from this method and then you can use that to populate data.

3 REPLIES 3

Former Member
0 Kudos

THere is a way.

Call method CREATE_DYNAMIC_TABLE of class CL_ALV_TABLE_CREATE.

All you need to do is build a field catalog as you build to a ALV display and you will get a reference of the interntal table as an export parameter from this method and then you can use that to populate data.

0 Kudos

Hi Sharath,

Sorry for my lateness.

My problem has been solved using Anirban Bhattacharjee's solution as the following link:

Link: [https://www.sdn.sap.com/irj/scn/wiki?path=/display/snippets/dynamicCreationofinternaltablebyRTTC]

Thank you for your answer.

Best regards,

Nguyen

0 Kudos

Seeing your FM code you can do it in a more simpler way as below:

DATA: text TYPE string VALUE 'SAP COMMUNITY NETWORK',
      itab TYPE TABLE OF string WITH HEADER LINE.

SPLIT text AT space INTO TABLE itab.

~Eswar