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: 

Extracting Field Name From a Variable

Former Member
0 Kudos


Hi Experts,

I have the following requirement from a client.

I have the name of the Field of Infotype 0008 var1 = 'LGA01'  and var2 = 'BET01' stored in a local variable.

The requirement is such that I need to change the value stored in 'BET01'.

Any help would be appreciated.

14 REPLIES 14

former_member183607
Contributor
0 Kudos

Hi,

     You may use Field Symbol like

     data : <FS> type any.

              

               var2 = 'BET01'.

               Assign var2 to <FS>.

               <FS> = change as required.

0 Kudos

I have an  internal table p0008.

It has a field BET01 which is stored in var1.

I need p0008-bet01 = Change required.

Regards

0 Kudos

Still question is not clear..

var1 = 'BET01'.

concatenate 'P0008-'  var1  into var2.

assign (var2) to <fs>.

<fs> = change required.

0 Kudos

Hi Sagar,

since we're deep in field-symbols already, do what sreekanth already suggested only maky a combination of both. This would be like:

FIELD-SYMBOLS: <FS_P0008> type P0008, <FS_BETNN> type BETRG.

You will have to assign your workarea ( in my example, i assume it is P0008) to FS <FS_P0008>

LOOP AT P0008 ASSIGNING <FS_P0008> WHERE something.

ASSIGN COMPONENT (var2) OF STRUCTURE <FS_P0008> TO <FS_BETNN>

* make your changes to <FS_BETNN>

<FS_BETNN> = new_value.  "DONE - BET01 has new value IN THE TABLE

ENDLOOP.

Remember whatever you do to a field-symbol, you do to the assigned variable or table-field.

In this example, you don't actually loop through your infotype table, but instead you move your memory-handle (that is, what a field-symbol is. Almost but not quite like a C-Pointer) over the lines of the table. Whichever line is linked to the field-symbol at that moment, will get the impact of change imediately.

The second filed-symbol gives you the flexibility to change BET02, BET03... as need be, by changing the value of var2.

Best regards - Jörg

former_member202818
Active Contributor
0 Kudos

Are you struggling with repetitive structures?

then do this.

DATA: s_lgart(14) type c value 'GWA_0008-LGA  ',

           s_betrg(14) type c value 'GWA_0008-BET  ',

           index(2) type n.

FIELD-SYMBOLS: <fs_lgart>  TYPE PA0008-LGA01,

                               <fs_betrg> TYPE PA0008-BET01.

do 40 times."Since 40 repetitive structures

    index = sy-index.            "01, 02, 03, 04...

    s_lgart+12(2) = index.    "GWA_0008-LGA01, GWA_0008-LGA02 ...

    s_betrg+12(2) = index.   "GWA_0008-BET01, GWA_0008-BET02...

    assing (s_lgart) to <fs_betrg>.     

    assing (s_betrg) to <fs_lgart>.

                                                         "<fs_betrg> and <fs_lgart> now store respective values LGA01,                                                                               BET01 (in first loop),                                                                                                                                 LGA02,BET02 (in second) and so on...

enddo.

0 Kudos

Hi Sreekanth,

     How do I change the value in the Internal table??

     I have only one row selected so that is not an issue. I need to change only the value found in BET01 present in internal table.

If change the value in Filed Symbols how will it reflect in my internal table??

0 Kudos

for that you have to read each record. if only BET01 has to change.

FIELD-SYMBOL : <fs_0008> TYPE P0008.

LOOP AT p0008 ASSIGNING <fs_0008>.


<fs_0008>-BET01 = change required.


endloop.

0 Kudos

Hi skreekanth,

The value of BET01 is also stored in a variable right.

So I can I do <fs_0008>-var1 =  change required.

Will this make changes in the Internal table as well??

0 Kudos

yes, I will explain the below statement..

LOOP AT p0008 ASSIGNING <fs_0008>. "For each loop <fs_0008>(like a pointer) will point to each record in p0008


<fs_0008>-BET01 = change required. "changing BET01 of current pointing record. After this statement internal table p0008 will get modified.


endloop.


Try this in debug mode. See the changes.

0 Kudos

Hi Sreekanth,

I have the value of BET01 in a variable

example Var1 = BET01. It is a dynamic value

I have to fetch it and is stored in a variable. It can be anythinh like BET01 or BET02 or LGAT01

Then I have to change the value stored in that particular colum

Fro instance if

Var1 = BET02

itab-BET02 = 10.

I have to change the value in itab-Bet02 = new value

Regards

0 Kudos

Hi Sagar,

                 Use Do varying it will help please refer to below link for example

DO 40 TIMES

  VARYING LGART FROM GWA_0008-LGA01 NEXT GWA_0008-LGA02

  VARYING BETRG FROM GWA_0008-BET01 NEXT GWA_0008-BET02.

  " CODE

ENDDO.

ABAP Keyword Documentation

Thanks.

0 Kudos

I want to know how to assign the column name that is stored in the variable to the internal table

0 Kudos

This message was moderated.

0 Kudos

Thanks a lot Experts