USING FIELD SYMBOLS IN PROGRAM:
The most useful thing a field symbol allows you to do is reference any field/variable on the fly by just putting the name of a field into a variable and assigning a field symbol to it.
For better understanding check the field symbol values in debugging, how the values changing
PROGRAM:
*&---------------------------------------------------------------------*
*& Report Z_FIELD_SYMBOLS
*&
*&---------------------------------------------------------------------*
REPORT Z_FIELD_SYMBOLS.
TYPES: BEGIN OF t_p0121,
pernr TYPE pa0121-pernr,
rfp01 TYPE pa0121-rfp01,
rfp02 TYPE pa0121-rfp02,
rfp03 TYPE pa0121-rfp03,
rfp04 TYPE pa0121-rfp04,
END OF t_p0121.
DATA: it_p0121 TYPE STANDARD TABLE OF t_p0121 INITIAL SIZE 0,
wa_p0121 TYPE t_p0121.
DATA: gd_index TYPE string,
gd_rfp0 TYPE string.
FIELD-SYMBOLS: <fs1>, <fs2>.
****************************************************************
*Start-of-selection.
START-OF-SELECTION.
SELECT pernr
rfp01
rfp02
rfp03
rfp04
UP TO 10 ROWS
FROM pa0121
INTO TABLE it_p0121.
****************************************************************
*End-of-selection.
END-OF-SELECTION.
WA_P0121-RFP01 = '1234'.
CONCATENATE 'WA_P0121-RFP01' gd_index INTO gd_rfp0.
* Now watch how the values change as you loop around the table fields
LOOP AT it_p0121 INTO wa_p0121.
write:/.
write:/ wa_p0121-pernr.
CLEAR: gd_index.
DO.
gd_index = gd_index + 1.
CONCATENATE 'WA_P0121-RFP0' gd_index INTO gd_rfp0.
* assign with brackets
ASSIGN (gd_rfp0) TO <fs1>. "assigns the value of field name contained in variable
* fs1 value would be the value of the field WA_P0121-RFP01..21
* i.e. if index 1 and WA_P0121-RFP01 = 1234 then fs1 would = 1234
* assign without brackets
ASSIGN gd_rfp0 TO <fs2>. " assigns the exact value contained in the field
* fs1 value would literally be the same as the field WA_P0121-RFP01..21
* i.e. if index 1 then fs2 would = 'WA_P0121-RFP01'
* index 2 then fs2 would = 'WA_P0121-RFP02' etc...
* you may also notice that once assigned any change made to the field gd_rfp0
* is instantly reflected in the field symbol (fs2) so technically you could perform
* the assign command once outside of the loop, but i have left it here to aid
* readability.
write:/ <fs2>, <fs1>.
IF gd_index GE 21. "exit once last field has been read
EXIT.
ENDIF.
ENDDO.
ENDLOOP.
OUTPUT:
The most useful thing a field symbol allows you to do is reference any field/variable on the fly by just putting the name of a field into a variable and assigning a field symbol to it.
For better understanding check the field symbol values in debugging, how the values changing
PROGRAM:
*&---------------------------------------------------------------------*
*& Report Z_FIELD_SYMBOLS
*&
*&---------------------------------------------------------------------*
REPORT Z_FIELD_SYMBOLS.
TYPES: BEGIN OF t_p0121,
pernr TYPE pa0121-pernr,
rfp01 TYPE pa0121-rfp01,
rfp02 TYPE pa0121-rfp02,
rfp03 TYPE pa0121-rfp03,
rfp04 TYPE pa0121-rfp04,
END OF t_p0121.
DATA: it_p0121 TYPE STANDARD TABLE OF t_p0121 INITIAL SIZE 0,
wa_p0121 TYPE t_p0121.
DATA: gd_index TYPE string,
gd_rfp0 TYPE string.
FIELD-SYMBOLS: <fs1>, <fs2>.
****************************************************************
*Start-of-selection.
START-OF-SELECTION.
SELECT pernr
rfp01
rfp02
rfp03
rfp04
UP TO 10 ROWS
FROM pa0121
INTO TABLE it_p0121.
****************************************************************
*End-of-selection.
END-OF-SELECTION.
WA_P0121-RFP01 = '1234'.
CONCATENATE 'WA_P0121-RFP01' gd_index INTO gd_rfp0.
* Now watch how the values change as you loop around the table fields
LOOP AT it_p0121 INTO wa_p0121.
write:/.
write:/ wa_p0121-pernr.
CLEAR: gd_index.
DO.
gd_index = gd_index + 1.
CONCATENATE 'WA_P0121-RFP0' gd_index INTO gd_rfp0.
* assign with brackets
ASSIGN (gd_rfp0) TO <fs1>. "assigns the value of field name contained in variable
* fs1 value would be the value of the field WA_P0121-RFP01..21
* i.e. if index 1 and WA_P0121-RFP01 = 1234 then fs1 would = 1234
* assign without brackets
ASSIGN gd_rfp0 TO <fs2>. " assigns the exact value contained in the field
* fs1 value would literally be the same as the field WA_P0121-RFP01..21
* i.e. if index 1 then fs2 would = 'WA_P0121-RFP01'
* index 2 then fs2 would = 'WA_P0121-RFP02' etc...
* you may also notice that once assigned any change made to the field gd_rfp0
* is instantly reflected in the field symbol (fs2) so technically you could perform
* the assign command once outside of the loop, but i have left it here to aid
* readability.
write:/ <fs2>, <fs1>.
IF gd_index GE 21. "exit once last field has been read
EXIT.
ENDIF.
ENDDO.
ENDLOOP.
OUTPUT: