Loading an internal table using dynamic data names

Sometimes we have different variables with homogenius contentthat should be in an internal table. Here's how to do it.

Sometimes we have different variables with homogenius content, and it would be preferable if those variables were

in an internal table. To do this we can use field-symbols. The code below was written in 4.6b and works with 4.0. Previous versions might require some edits.

Report ZTest.

Parameters: dat01(5),
            dat02(5),
            dat03(5),
            dat04(5),
            dat05(5),
            dat06(5),
            mat01 like mara-matnr,
            mat02 like mara-matnr,
            mat03 like mara-matnr,
            mat04 like mara-matnr,
            mat05 like mara-matnr,
            mat06 like mara-matnr.            

types: begin of t_itab,
         dat(5),
         mat     like mara-matnr,
       end of t_itab.

data: itab type standard table of t_itab with header line.

Start-of-selection.
  Perform fr_load_itab.
  loop at itab.
    write:/ itab-dat, itab-mat.
  endloop.

******************************************************************
Form fr_load_itab.
Data: c_dat(3)   VALUE 'DAT', "Constant field name
      c_mat(3)   VALUE 'MAT', "Constant field name
      w_i(2)     Type N,      "Index name
      w_field(5).             "Target field name

* Field Symbols
  Field-Symbols: <FS1> TYPE ANY,
                 <FS2> TYPE ANY.

  clear: w_i, itab.
  While w_i < 4.
    w_i = w_i + 1.

    clear w_field.
    concatenate c_dat w_i INTO W_FIELD.
    condense W_FIELD.                    "Create first field name
    ASSIGN (W_FIELD) TO <FS1>.           "Assign field value
    itab-dat = <FS1>.

    clear w_field.
    concatenate c_mat w_i INTO W_FIELD.
    condense W_FIELD.                    "Create second field name
    ASSIGN (W_FIELD) TO <FS2>.           "Assign field value
    itab-mat = <FS2>.

    APPEND itab.   
    clear itab.
  EndWhile.

Endform.

This was first published in August 2002

Dig deeper on SAP ABAP

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close