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: 

how to get field names dynamically from an internal table

Former Member
0 Kudos

Hi,

I am having an internal table with fields

balance

a1

a2

a3

a4

a5

a6

amount

all these fields has value except balance.

i need to calculate balance for each record in this internal table.

on the selection screen user will give a1 to a4 or a3 to a6 or a2 to a5 etc as an input.

The logic for calculating balance is if user has given input as a2 to a5 then

balance = a2 + a3 + a4 + a5 + amount.

if user gives input as a1 to a4 then

balance = a1 + a2 + a3 + a4 + amount.

so dynamically i need to change the field names of the internal table depending upon the user input to calculate balance.

can anyone explain me how to get those field values dynamically from the internal table. Thanks...

Regards,

Rose.

5 REPLIES 5

Sm1tje
Active Contributor
0 Kudos

Maybe not quite answer to your question, but this is how I would try and do it.

Determine the number from the input like this:

A1 to A5, determine numbers 1 and 5.

next between DO and ENDDO use ASSIGN COMPONENT lv_index OF STRUCTURE (line of itab) TO <fs>.

lv_index_start = 1.

lv_index_end = 5.

DO.

ASSIGN .....

IF SY-INDEX > lv_index_end.

EXIT.

ELSE.

lv_balance = lv_balance + <fs>.

ENDIF.

ENDDO.

Something like this perhaps?

Edited by: Micky Oestreich on Apr 17, 2008 3:54 PM

Former Member
0 Kudos

Hye rose,

I did not understand the exact requirement of yours. But here is the procedure to create a dynamic work area and an internal table.

The itab contains the fields or selected coloumns between a1 to a6 in your case.

FIELD-SYMBOLS : <it_final> TYPE STANDARD TABLE,

<wa_final> TYPE ANY,

<w_field> TYPE ANY.

LOOP AT itab.

wa_fieldcatalog-fieldname = itab-field.

wa_fieldcatalog-outputlen = '14'.

APPEND wa_fieldcatalog TO it_fieldcatalog.

ENDLOOP.

*******CREATE DYNAMIC TABLE***********************

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcatalog

IMPORTING

ep_table = new_table

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

IF sy-subrc 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

ASSIGN new_table->* TO <it_final>.

********CREATE WORK AREA***************************

CREATE DATA new_line LIKE LINE OF <it_final>.

ASSIGN new_line->* TO <wa_final>.

Reward if helpful.

Thanks,

Imran.

Sm1tje
Active Contributor
0 Kudos

Come to think of it, if you can determine the actual name of the parameter on the selection screen,

you can also use this in a variable and do the assign like this:

ASSIGN COMPONENT lv_name OF STRUCTURE.

lv_name will be A1 or A2. This only works of parameter name and name of itab column are the same!!

former_member194669
Active Contributor
0 Kudos

Try this way


select-options : s_period like .... " as your input field for A1 - A5 entry

data : v_field type dd03l-fieldname.

" This will give itab structure in internal table
call function 'GET_COMPONENT_LIST'
     exporting
       program    = v_repid  " Your report name
       fieldname  = v_fname  " itab name or structure name ie ITAB
     tables
       components = i_components.

data : v_first_sequence like sy-tabix.
data : v_last_sequence like sy-tabix.

" Find position in the ITAB
read table i_components with key compname = s_period-low.
 if sy-subrc eq 0.
     move sy-tabix to v_first_sequence.
 endif.
read table i_components with key compname = s_period-high.
 if sy-subrc eq 0.
    move sy-tabix to v_last_sequence.
 endif.


" Then in the ITAB starting from first seqeunce postion to 
" Last sequence find the total amount and modify itab-balance.

loop at itab.
v_tabix = v_first_sequence.
do,
   assign component v_tabix of structure itab to <fs_val>.
   move <fs_val> to v_amount. 
   itab-balance = itab-balance + v_amount. 
   if v_tabix eq v_last_sequence.
    itab-balance = itab-balance + itab-amount.
    modify itab.
    exit.
   endif.
   v_tabix = v_tabix + 1.
enddo.
endloop.

Former Member
0 Kudos

hi phyrose,

suppose consider itab has a internal table.

suppose u r input is 1- 4

num1 =1 num2 = 4

num2 = num2 - num1 + 1

data:

str(5) type c,

str1(6) type c..

loop at itab.

do num2 times.

str = 'itab-a'.

concatenate str num1 into str1.

itab-balance + = (str1).

num1 + =1.

endloop.