Tips and Tricks for the ABAP® Programming Language

Y9020021: Assigning N-fields (numbers)

Partial association
In this example, three N-fields are associated to a field symbol with “ASSIGN”. Each of the three N-fields is associated partially only to the same field symbol.

Relative addressing by numeric values
In examples 1 – 3, constant numeric values are used for the addressing of the data field.

Relative addressing by addressing fields
In example 4, the addressing is modified continuously in a do-loop. The thus addressed part of the data field is associated to the field symbol and evaluated.

Modification only in the addressed part
If only a part of a data field was associated to a field symbol and the field symbol is used in a memory modifying command, the effects are constrained to the addressed part of the origin field.

Addressing other N-fields
By specification of an appropriate offset, other N-fields can be associated (and hence be modified) as well.

ABAP-Source-Code

You can cut and paste the source code directly into the ABAP-Workbench.

REPORT Y9020021 LINE-SIZE 130.  "Release 3.1G, 4.5A
************************************************************************
* Copyright (c) 1999 by CT-Team, 33415 Verl, http://www.ct-software.com
*
*     You can use or modify this report for your own work as long
*               as you don't try to sell or republish it.
*      In no event will the author be liable for indirect, special,
*        Incidental, or consequental damages (if any) arising out of
*                       the use of this report.
*
************************************************************************
 BREAK-POINT.
*//////////////////////////////////////////////////////////////////////*
 MOVE: 'TESTREPORT  "ASSIGN  NFeld+o(l)  TO  <fs1>"        '
        TO SY-TITLE.
*//////////////////////////////////////////////////////////////////////*
***************       Declaration of variables    **********************
 FIELD-SYMBOLS <FS1>.
*.......................................................................
 DATA: NFELD7(7)   TYPE N VALUE '345678'.
 DATA: NFELD15(15) TYPE N VALUE '777777700002222'.
 DATA: NFELD25(25) TYPE N VALUE '   33333  111111 44444 77'.
*
 DATA: OFFX TYPE I VALUE 0.
 DATA: LENX TYPE I VALUE 5.
*
*//////////////////////////////////////////////////////////////////////*
*************            -  Main Section             *******************
*//////////////////////////////////////////////////////////////////////*
*
 SKIP.
 ASSIGN NFELD7+2(5) TO <FS1>.     "ok
*i A part of NFELD7 will be assign to field symbol <FS1>.
*.......................................................................
   BREAK-POINT 'Example-1'.
   WRITE: / 'ASSIGN command with NFELD7' COLOR 4.
   PERFORM DISPLAY-NFELD USING NFELD7 'NFeld7'.
   ULINE. SKIP 2.
*-----------------------------------------------------------------------
 ASSIGN NFELD15+2(5) TO <FS1>.     "<-- A part of NFELD will be assigned
*.......................................................................
   BREAK-POINT 'Example-2'.
   WRITE: / 'ASSIGN command with NFELD15' COLOR 5.
   PERFORM DISPLAY-NFELD USING NFELD15 'NFeld15'.
   ULINE. SKIP 2.
*-----------------------------------------------------------------------
 ASSIGN NFELD25+2(5) TO <FS1>.     "<-- A part of NFELD will be assigned
*.......................................................................
   BREAK-POINT 'Example-3'.
   WRITE: / 'ASSIGN command with NFELD25' COLOR 6.
   PERFORM DISPLAY-NFELD USING NFELD25 'NFeld25'.
   ULINE. SKIP 2.
*-----------------------------------------------------------------------
*...................... a little complicated ...........................
   BREAK-POINT 'Example-4'.
   SKIP.
   WRITE: / 'ASSIGN command (Offset and length as fields) with NFELD25'.
   NFELD25 = '1111122222333334444455555'.
     DO 5 TIMES.
       WRITE: /5 'DO loop number:' COLOR 5, SY-INDEX.
       ASSIGN NFELD25+OFFX(LENX) TO <FS1>.
       PERFORM DISPLAY-NFELD USING NFELD25 'NFeld25'.
       SKIP 4.
       ADD 5 TO OFFX.
*      ADD 1 TO LENX.
     ENDDO.
*.......................................................................
*  In this loop the NFELD25 will be assigned bit by bit to the
*  field symbol <FS1>, after that the field symbol <FS1> will be changed
*  in another subroutine. You see that NFELD25 will be also changed.
*
*//////////////////////////////////////////////////////////////////////*
*************                Subroutines             *******************
*//////////////////////////////////////////////////////////////////////*
*
************************************************************************
*            Display of C fields and field symbols                     *
************************************************************************
 FORM DISPLAY-NFELD USING NFELD FNAME.
*
 WRITE: /10 'Content of', FNAME, ':', NFELD.
 PERFORM FELDEIGENSCHAFTEN USING NFELD.
*
 WRITE: /10 'Content of <FS1> aufgrund der Teilzuordnung :', <FS1>.
 PERFORM FELDEIGENSCHAFTEN USING <FS1>.
*-----------------------------------------------------------------------
 MOVE 88880   TO <FS1>.               "<-- Das Feldsybmol wird verwendet
*-----------------------------------------------------------------------
 WRITE: /10 'MOVE 88880 TO <FS1>'.
 ULINE.
*.......................................................................
 WRITE: /10 'Content of', FNAME, 35 ':', NFELD.
 WRITE: /10 'Content of <FS1>',  35 ':', <FS1>.
*.......................................................................
 ENDFORM.
************************************************************************
*    Determination of field properties (only for information)          *
************************************************************************
 FORM FELDEIGENSCHAFTEN USING ALLG.
*
 DATA: FLAENGE(2) TYPE N.
 DATA: FTYP(1) TYPE C.
 DATA: FOUT(2) TYPE N.
 DATA: FDEZ(2) TYPE N.
*.......................................................................
  ULINE.
  DESCRIBE FIELD ALLG LENGTH FLAENGE.
  WRITE: /10 'Field length  :', FLAENGE.
*
  DESCRIBE FIELD ALLG TYPE FTYP.
  WRITE: /10 'Field type    :', FTYP.
*
  DESCRIBE FIELD ALLG OUTPUT-LENGTH FOUT.
  WRITE: /10 'Output length :', FOUT.
*
  DESCRIBE FIELD ALLG DECIMALS FDEZ.
  WRITE: /10 'Decimals      :', FDEZ.
* SKIP 3.
*.......................................................................
 ENDFORM.
************************************************************************
************************************************************************
******************* END OF PROGRAM *************************************