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

can we access a field in an internal table field using variable

Former Member
0 Likes
655

Hi all,

I have a requirement like this..

Let we have an internal table with fields d01, d02 and so on

we build this field name at runtime like in a loop

I can make the VAR value to d01 at runtime like concatenate 'd' syindex into VAR

then we assign a value to that field like itab-d01 at runtime

now I want to assign itab-VAR = xyz

here it shoud take itab-d01 = xyz as VAR = d01

hope explained the requirement properly?

Please help me in this issue.

Regards,

Ravi

Hi all,

I have a requirement like this..

Let we have an internal table with fields d01, d02 and so on

we build this field name at runtime like in a loop

I can make the VAR value to d01 at runtime like concatenate 'd' syindex into VAR

then we assign a value to that field like itab-d01 at runtime

now I want to assign itab-VAR = xyz

here it shoud take itab-d01 = xyz as VAR = d01

hope explained the requirement properly?

Please help me in this issue.

Regards,

Ravi

2 REPLIES 2
Read only

Former Member
0 Likes
563

Hi,

u an do like this.


  DATA: BEGIN OF STR,
          A VALUE 'a',
          B VALUE 'b',
          C VALUE 'c',
          D VALUE 'd',
        END   OF STR,
        CN(5) VALUE 'D'.
  FIELD-SYMBOLS <FS> TYPE ANY.
  DO 3 TIMES.
    ASSIGN COMPONENT SY-INDEX OF
           STRUCTURE STR TO <FS>.
    IF SY-SUBRC <> 0. EXIT. ENDIF.
    WRITE <FS>.
  ENDDO.
  ASSIGN COMPONENT CN OF STRUCTURE STR TO <FS>.
  WRITE <FS>.

   move '1' to <fs>.
  WRITE str-d.

your case.


  DATA: BEGIN OF STR OCCURS 0,
          d01(5),
          d02(5),
          d03(5),
          d04(5),
        END   OF STR,
        CN(5) VALUE 'd04'.
  DATA:ind(2).
  FIELD-SYMBOLS <FS> TYPE ANY.

  DO 4 TIMES.
    ASSIGN COMPONENT SY-INDEX OF
           STRUCTURE STR TO <FS>.
    IF SY-SUBRC <> 0.
      EXIT.
    ENDIF.
    ind = sy-index.
    CONCATENATE 'val' ind into <FS>.
  ENDDO.
  APPEND str.

  DO 4 TIMES.
    ASSIGN COMPONENT SY-INDEX OF
               STRUCTURE STR TO <FS>.
    write: <fs>.
  ENDDO.

    ASSIGN COMPONENT CN OF STRUCTURE STR TO <FS>.
    WRITE:/ <FS>.

rgds,

bharat.

Read only

0 Likes
563

Hi Bharat,

Thank you for the reply.

Hope that I have not explained the requirement well,

Please check the program below


data: begin of itab occurs 0,
  	d01 type char2,
	d02 type char2,
  	d03 type char2,
       end of itab.

data: begin of jtab occurs 0,
	item type num2,
	value type char2,
       end of jtab.


jtab-item = 01.
jtab-value = 'na'.
append jtab.

jtab-item = 02.
jtab-value = 'nb'.
append jtab.

jtab-item = 03.
jtab-value = 'zy'.
append jtab.

do 3 times.
  concatenate 'd' sy-index into field.

  read table jtab with key item = sy-index.
    if sy-subrc = 0.
      itab-(field) = jtab-value. "<< here I want actually to assign to d01, d02 and to d03.
    endif.
enddo.

append itab.

* here actually 3 records in jtab will create only one record in itab.

Like this I want to assign the values dynamically to fill itab from the values of jtab.

work around to this will be


data: begin of itab occurs 0,
	d01 type char2,
	d02 type char2,
	d03 type char2,
       end of itab.

data: begin of jtab occurs 0,
	item type num2,
	value type char2,
       end of jtab.


jtab-item = 01.
jtab-value = 'na'.
append jtab.

jtab-item = 02.
jtab-value = 'nb'.
append jtab.

jtab-item = 03.
jtab-value = 'zy'.
append jtab.

do 3 times.

  concatenate 'd' sy-index into field.
  read table jtab with key item = sy-index.
  if sy-subrc = 0.
    case sy-index.
      when '01'.
        itab-d01 = jtab-value. 
      when '02'.
        itab-d02 = jtab-value. 
      when '03'.
        itab-d03 = jtab-value. 
    endcase.

  endif.
enddo.

append itab.

In the above case only 3 fields are there if more fields are there it will be cumbersome to handle all the situations by code.

Hope now you understood the requierement.

Regards,

Ravi