Tips and Tricks for the ABAP® Programming Language
Y9020020 – Relative addressing of C fields (strings)
- Partial association In this example, three C-fields are associated to a field symbol with “ASSIGN”. Each of the three C-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. The command CFELD50, used in the do-loop, is modified several times, because the association to the field symbol is varied permanently. With this technique, e.g. a string can be processed comparably simple.
- Addressing other C-fields By specification of an appropriate offset, other C-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 Y9020020 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 CFeld+o(l) TO <fs1>" ' TO SY-TITLE. *//////////////////////////////////////////////////////////////////////* *************** Deklaration of variables ********************** FIELD-SYMBOLS <FS1>. *....................................................................... DATA: CFELD7(7) TYPE C VALUE 'AUGUST'. DATA: CFELD25(25) TYPE C VALUE 'EGON, Hugo, heinrich'. DATA: CFELD50(50) TYPE C VALUE 'This is a small test string 123 ABCDE'. * DATA: OFFX TYPE I VALUE 4. DATA: LENX TYPE I VALUE 5. * *//////////////////////////////////////////////////////////////////////* ************* Main Section ******************* *//////////////////////////////////////////////////////////////////////* * SKIP. ASSIGN CFELD7+2(5) TO <FS1>. "=ok * Only a part of CFELD7 (CFIELD7) is assigned to field symbol <FS1> . * *############ you can uncomment the following command ############ * ASSIGN CFELD7+32(5) TO <FS1>. * *i In this suggestion the field symbol <FS1> points already to *i another variable (CFELD50) und this is in the most cases not wanted *i *############ you can uncomment the following command ############ * ASSIGN CFELD7+2 TO <FS1>. * *i Here the field CFELD7 with a length of 7 Bytes would be assigned to *i the field symbol <FS1>. The result can be a change of a foreign *i memory area !! *####################################################################### *....................................................................... BREAK-POINT 'Example-1'. WRITE: / 'ASSIGN command with CFELD7' COLOR 6. PERFORM DISPLAY-CFELD USING CFELD7 'CFeld7'. ULINE. SKIP 2. *----------------------------------------------------------------------- ASSIGN CFELD25+2(5) TO <FS1>. "<-- A part of CFELD is assigned *....................................................................... BREAK-POINT 'Example-2'. WRITE: / 'ASSIGN command with CFELD25' COLOR 6. PERFORM DISPLAY-CFELD USING CFELD25 'CFeld25'. ULINE. SKIP 2. *----------------------------------------------------------------------- ASSIGN CFELD50+2(5) TO <FS1>. "<-- A part of CFELD is assigned *....................................................................... BREAK-POINT 'Example-3'. WRITE: / 'ASSIGN command with CFELD50' COLOR 6. PERFORM DISPLAY-CFELD USING CFELD50 'CFeld50'. ULINE. SKIP 2. *----------------------------------------------------------------------- *...................... a little complicated ........................... BREAK-POINT 'Example-4'. WRITE: / 'ASSIGN command (offset and length as fields) with CFELD50' COLOR 6. SKIP. DO 6 TIMES. WRITE: /5 'DO loop number :' COLOR 4, SY-INDEX. ADD 7 TO OFFX. * ADD 1 TO LENX. ASSIGN CFELD50+OFFX(LENX) TO <FS1>. PERFORM DISPLAY-CFELD USING CFELD50 'CFeld50'. SKIP 4. ENDDO. *....................................................................... * *//////////////////////////////////////////////////////////////////////* ************* subroutines ******************* *//////////////////////////////////////////////////////////////////////* * ************************************************************************ * Display of C fields and field symbols * ************************************************************************ FORM DISPLAY-CFELD USING CFELD FNAME. * WRITE: /10 'Content of ', FNAME, ':', CFELD. PERFORM FELDEIGENSCHAFTEN USING CFELD. * WRITE: /10 'Content of <FS1> is a result of partial assign :', <FS1>. PERFORM FELDEIGENSCHAFTEN USING <FS1>. *----------------------------------------------------------------------- MOVE 'HENRY' TO <FS1>. "<-- The field symbol is used *----------------------------------------------------------------------- WRITE: /10 'MOVE ''HENRY'' TO <FS1>'. ULINE. *....................................................................... WRITE: /10 'Content of', FNAME, 35 ':', CFELD. 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 *************************************