Tips and Tricks for the ABAP® Programming Language

Y9020025 – Relative Addressing of P fields / Problems (packed content)

  1. Partial association (“not good for P fields …”) In this example, three P-fields are associated to a field symbol with “ASSIGN”. Each of the three P-fields is associated partially only to the same field symbol. The following informations are somewhat difficult to integrate for “beginners”.
  2. Relative addressing of a P-field Basically, it can be said that P-fields must generally not be relatively addressed and be associated to a field symbol, except when using the same field length of the origin field as length and a multiple of the length as offset. This way, e.g. a field string with P-fields of equal length can be processed quite simple by a field symbol. The drawback: Once in a while, “trash” is produced by addressing errors, sometimes this is unnoticed at first.
  3. Example 1 – Here, a relative addressing is chosen that would cause a runtime error if it were activated.
  4. Example 2 – PFELD4 is addressed with “PFELD4+4(4)” and associated to the field symbol . This way, the field PFELDX, following next in the declaration, is addressed however. So, with a modification of , PFELDX is modified.
  5. Example 3 – PFELD4 is addressed with “PFELD4+8(4)” and associated to the field symbol . This way, the field PFELD6, following next in the declaration, is addressed however. So, with a modification of , PFELD6 is modified. The field symbol has received the length (= 4) and the type (= P) from PFELD4. So it modifies its associated data field only in a length of 6. Associated, however, is not PFELD4, but PFELD6, so the sign is placed “right into the field”. Result in PFELD6: ” 32.3<4.00 “.
  6. Example 4 – The “unintentional” error in example 3 has replaced the initial value of PFELD6 by an invalid value. However, processing can be continued, but should not. The value 111 is added to the associated field symbol (everything remains quiet) and already PFELD6 has a “new value” again that even looks quite suitable. Unfortunately, this is all bogus.
  7. Example 5 – Here, PFELD8 is relatively addressed under assistance of two variables (OFFX and LENX). Unfortunately, the content of OFFX is not as planned before. So the program is going its own way. PFELD8 is addressed relatively, but the pointer references field CFELD8. This field contains a bunch of beautiful AAAA… On the field symbol, that possesses a length of 8 bytes and the data type P, based of the origin field PFELD8, the value 111 is added. The effects are to be viewed not in PFELD8, but in CFELD8. “AAAAAAAA” has become “AAAARQAL”. Ergo: general caution with relative addressing.

ABAP-Source-Code

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

REPORT Y9020025 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 for    "ASSIGN  PFeld+o(l)  TO  <fs1>"        '
        TO SY-TITLE.
*//////////////////////////////////////////////////////////////////////*
***************       Declaration of variables    **********************
 FIELD-SYMBOLS <FS1>.
*.......................................................................
 DATA: PFELD4(4)   TYPE P VALUE '1022333'.
 DATA: PFELDX(4)   TYPE P VALUE '7777777'.
 DATA: PFELD6(6)   TYPE P VALUE '21244' DECIMALS 2.
 DATA: PFELD8(8)   TYPE P VALUE '7333213' DECIMALS 4.
 DATA: CFELD8(8)   TYPE C VALUE 'AAAAAAAA'.
*
 DATA: OFFX    TYPE I VALUE 8.
 DATA: LENX    TYPE I VALUE 8.
*//////////////////////////////////////////////////////////////////////*
*************               Main Section             *******************
*//////////////////////////////////////////////////////////////////////*
*
    WRITE: /5  'Example 1,  *** Inadmissible ASSIGN ***' COLOR 6.
    WRITE: /10 'Inadmissible  ASSIGN: ''ASSIGN PFELD4+1(3) TO <FS1>'' '.
*   ASSIGN PFELD4+1(3) TO <FS1>.
    ULINE. SKIP 2.
************************************************************************
*
    BREAK-POINT.
    WRITE: / 'ASSIGN command with PFELD4, but PFELDX will be assigned'
              COLOR 3.
    WRITE: /5 'Example 2'.
    PERFORM DISPLAY-PFELD USING PFELD4 'PFELD4'.
    WRITE: /10 'Content of   PFELDX      :', PFELDX.
    ULINE. SKIP 2.
*-----------------------------------------------------------------------
 ASSIGN PFELD4+8(4) TO <FS1>.
************************************************************************
*
    BREAK-POINT.
    WRITE: / 'ASSIGN with PFELD4, but PFELD6 is also partly affected !'
              COLOR 3.
    WRITE: /5 'Example 3'.
    WRITE: /10 'Content of   PFELD6      :', PFELD6.
    PERFORM DISPLAY-PFELD USING PFELD4 'PFELD4'.
    WRITE: /10 'Content of   PFELD6      :', PFELD6.
    ULINE. SKIP 2.
*-----------------------------------------------------------------------
 ASSIGN PFELD6 TO <FS1>.
************************************************************************
*
    BREAK-POINT.
    WRITE: / 'ASSIGN command with PFELD6' COLOR 3.
    WRITE: /5 'Example 4'.
    ULINE. SKIP 2.
    PERFORM DISPLAY-PFELD USING PFELD6 'PFELD6'.
*-----------------------------------------------------------------------
 ASSIGN PFELD8+OFFX(LENX) TO <FS1>.
************************************************************************
*
    BREAK-POINT.
    SKIP 2.
    WRITE: / 'ASSIGN command with PFELD8 but CFELD8 is affected'
              COLOR 3.
    WRITE: /5 'Example 5'.
    WRITE: /10 'Content of   CFELD8      :', CFELD8.
    PERFORM DISPLAY-PFELD USING PFELD8 'PFELD8'.
    WRITE: /10 'Content of   CFELD8      :', CFELD8.
    ULINE. SKIP 2.
*.......................................................................
*
*//////////////////////////////////////////////////////////////////////*
*************                Subroutines             *******************
*//////////////////////////////////////////////////////////////////////*
*
************************************************************************
*            Display of data fields and field symbols                  *
************************************************************************
 FORM DISPLAY-PFELD USING PFELD FNAME.
*
 WRITE: /10 'Content of', FNAME, ':', PFELD.
 PERFORM FELDEIGENSCHAFTEN USING PFELD.
*
 WRITE: /10 'Content of <FS1> :', <FS1>.
 PERFORM FELDEIGENSCHAFTEN USING <FS1>.
*-----------------------------------------------------------------------
 ADD   111    TO <FS1>.               "<-- The field symbol will be used
*-----------------------------------------------------------------------
 WRITE: /10 'ADD 111 TO <FS1>'.
 ULINE.
*.......................................................................
 WRITE: /10 'Content of', FNAME, 35 ':', PFELD.
 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 1.
*.......................................................................
 ENDFORM.
************************************************************************
************************************************************************
******************* END OF PROGRAM *************************************