From dadf4de59c64848cb40fa34f415103999ced7ad2 Mon Sep 17 00:00:00 2001 From: danrega <16720986+danrega@users.noreply.github.com> Date: Fri, 23 May 2025 17:54:14 +0200 Subject: [PATCH] Update --- 06_Dynamic_Programming.md | 355 ++++++++++++++++++++++++++++++++------ 1 file changed, 305 insertions(+), 50 deletions(-) diff --git a/06_Dynamic_Programming.md b/06_Dynamic_Programming.md index 39f4aaa..4739276 100644 --- a/06_Dynamic_Programming.md +++ b/06_Dynamic_Programming.md @@ -151,58 +151,313 @@ ENDLOOP. statements assign the memory area of a data object to a field symbol. Once the memory area is assigned, you can work with the content. -``` abap -"Some data object declarations to be used -DATA: num TYPE i, - struc TYPE zdemo_abap_fli, "Demo database table - itab_str TYPE string_table, - itab_fli TYPE TABLE OF zdemo_abap_fli WITH EMPTY KEY. -APPEND INITIAL LINE TO itab_fli. - -"Declaring field symbols with complete types -FIELD-SYMBOLS: TYPE i, - TYPE zdemo_abap_fli, - TYPE string_table. - -"Declaring field symbols with generic type -FIELD-SYMBOLS TYPE data. - -"Assigning data objects to field symbols -"Using field symbols with a static type -ASSIGN num TO . -ASSIGN struc TO . -ASSIGN itab_str TO . -"Using field symbol with a generic type -ASSIGN num TO . -ASSIGN itab_fli TO . -ASSIGN itab_fli[ 1 ] TO . -"Assigning components -ASSIGN struc-carrid TO . -ASSIGN itab_fli[ 1 ]-connid TO . - -"Inline declaration (the field symbol has the type data) -ASSIGN num TO FIELD-SYMBOL(). - -"CASTING addition for matching types of data object and field -"symbol when assigning memory areas -TYPES c_len_3 TYPE c LENGTH 3. -DATA(chars) = 'abcdefg'. -FIELD-SYMBOLS TYPE c_len_3. - -"Implicit casting -ASSIGN chars TO CASTING. "abc - -FIELD-SYMBOLS TYPE data. - -"Explicit casting -ASSIGN chars TO CASTING TYPE c_len_3. "abc - -DATA chars_l4 TYPE c LENGTH 4. -ASSIGN chars TO CASTING LIKE chars_l4. "abcd -``` +The table below includes a selection of `ASSIGN` statements, primarily featuring static assignments. More additions are covered further down. For more information, see the [ABAP Keyword Documentation](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/ABENSET_FIELD_SYMBOLS.html). > **💡 Note**
-> See more information on the addition `CASTING` [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapassign_casting.htm). +> In the event of an unsuccessful static assignment, `sy-subrc` remains unchanged (with exceptions), and no memory area is allocated to the field symbol. After the statement, the field symbol is unassigned. The addition `ELSE UNASSIGN`, although it cannot be explicitly specified in static assignments, is used implicitly. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Subject Example
+ +Assigning memory area to an existing field symbol with [complete data type](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abencomplete_data_type_glosry.htm) + + + + +``` abap +DATA str_a TYPE string VALUE `ABAP`. +DATA int_a TYPE i VALUE 123. +DATA struc_a TYPE i_timezone. +DATA tab_a TYPE string_table. + +FIELD-SYMBOLS TYPE string. +FIELD-SYMBOLS TYPE i. +FIELD-SYMBOLS TYPE i_timezone. +FIELD-SYMBOLS LIKE tab_a. + +ASSIGN str_a TO . +ASSIGN int_a TO . +ASSIGN struc_a TO . +ASSIGN tab_a TO . +``` + +
+ +Assigning memory area to an existing field symbol with [generic data type](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abengeneric_data_type_glosry.htm) + + + + +``` abap +DATA str_b TYPE string VALUE `ABAP`. +DATA int_b TYPE i VALUE 123. +DATA dec34_b TYPE decfloat34 VALUE '0.123'. +DATA struc_b TYPE i_timezone. +DATA strtab_b TYPE string_table. +DATA hashtab_b TYPE string_hashed_table. + +FIELD-SYMBOLS TYPE any. +FIELD-SYMBOLS TYPE numeric. +FIELD-SYMBOLS TYPE clike. +FIELD-SYMBOLS TYPE ANY TABLE. + +ASSIGN str_b TO . +ASSIGN int_b TO . +ASSIGN dec34_b TO . +ASSIGN struc_b TO . +ASSIGN strtab_b TO . +ASSIGN hashtab_b TO . + +ASSIGN int_b TO . +ASSIGN dec34_b TO . + +ASSIGN str_b TO . +ASSIGN struc_b TO . + +ASSIGN strtab_b TO . +ASSIGN hashtab_b TO . +``` + +
+ +Assigning memory area to a field symbol declared inline + + + + +``` abap +DATA str_c TYPE string VALUE `ABAP`. +DATA int_c TYPE i VALUE 123. +DATA strtab_c TYPE string_table. + +ASSIGN str_c TO FIELD-SYMBOL(). +ASSIGN int_c TO FIELD-SYMBOL(). + +LOOP AT strtab_c ASSIGNING FIELD-SYMBOL(). + ... +ENDLOOP. + +READ TABLE strtab_c ASSIGNING FIELD-SYMBOL() INDEX 1. +``` + +
+ +Assigning components + + + + +``` abap +DATA: BEGIN OF struc_d, + text TYPE c LENGTH 10, + num TYPE i, + END OF struc_d. +DATA tab_d LIKE TABLE OF struc_d WITH EMPTY KEY. +struc_d = VALUE #( text = 'ABAP' num = 1 ). +APPEND struc_d TO tab_d. + +FIELD-SYMBOLS LIKE struc_d-text. + +ASSIGN struc_d-text TO . + +"Note: With table expressions, sy-subrc is set. +ASSIGN tab_d[ 1 ]-num TO FIELD-SYMBOL(). +ASSERT sy-subrc = 0. + +ASSIGN tab_d[ 2 ]-num TO . +ASSERT sy-subrc <> 0. +``` + +
+ +Offset and length specifications + + + +Find more information [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/ABAPASSIGN_MEM_AREA_STATIC_DOBJ.html). + +
+ +``` abap +DATA txt_e TYPE c LENGTH 10 VALUE '0123456789'. +TYPES c1 TYPE c LENGTH 1. +FIELD-SYMBOLS TYPE c1. + +DO 11 TIMES. + DATA(off) = sy-index - 1. + ASSIGN txt_e+off(1) TO . + + IF sy-index < 11. + ASSERT IS ASSIGNED. + ELSE. + ASSERT IS NOT ASSIGNED. + ENDIF. +ENDDO. + +"The following example explores area limits of data objects that are assigned. +"The first ASSIGN statement assigns the area limits of the data object txt_e to a field symbol. +"In the second ASSIGN statement in a loop, the second field symbol takes over the area limits. +"This ASSIGN statement does not specify the offset. +"From a specific loop pass on, the assignment does not work anymore as a larger memory area +"is assigned. Therefore, the logical expression is then false. + +FIELD-SYMBOLS: TYPE any, + TYPE any. +"345 +ASSIGN txt_e+3(3) TO . + +DATA strtab_e TYPE string_table. +DO 10 TIMES. + ASSIGN (sy-index) TO . + IF IS ASSIGNED. + APPEND |{ } / sy-index = { sy-index }| TO strtab_e. + ELSE. + APPEND |Field symbol not assigned / sy-index = { sy-index }| TO strtab_e. + ENDIF. +ENDDO. + +*strtab_e table content: +*3 / sy-index = 1 +*34 / sy-index = 2 +*345 / sy-index = 3 +*3456 / sy-index = 4 +*34567 / sy-index = 5 +*345678 / sy-index = 6 +*3456789 / sy-index = 7 +*Field symbol not assigned / sy-index = 8 +*Field symbol not assigned / sy-index = 9 +*Field symbol not assigned / sy-index = 10 +``` + +
+ +`CASTING` addition for matching types of data object and field symbol + + + +Find more information [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapassign_casting.htm). + +
+ +``` abap +DATA txt1_f TYPE c LENGTH 7 VALUE 'abcdefg'. +TYPES c3 TYPE c LENGTH 3. +FIELD-SYMBOLS TYPE c3. + +"Implicit casting +"abc +ASSIGN txt1_f TO CASTING. + +FIELD-SYMBOLS TYPE data. + +"Explicit casting +"abc +ASSIGN txt1_f TO CASTING TYPE c3. + +DATA txt2_f TYPE c LENGTH 4. +"abcd +ASSIGN txt1_f TO CASTING LIKE txt2_f. +``` + +
+ +Assigning writable expressions + + + + +``` abap +"Table expressions +"Note: sy-subrc is set. If the line is found, sy-subrc is set to 0. + +DATA(strtab_f) = VALUE string_table( ( `ABAP` ) ). + +ASSIGN strtab_f[ 1 ] TO FIELD-SYMBOL(). +ASSERT sy-subrc = 0. + +ASSIGN strtab_f[ 2 ] TO . +ASSERT sy-subrc <> 0. + +"Constructor expressions + +"NEW +"Assigning instance attributes of a class +ASSIGN NEW zcl_demo_abap_objects( )->another_string TO FIELD-SYMBOL(). + +"CAST +TYPES: BEGIN OF struc_g, + comp1 TYPE i, + comp2 TYPE i, + END OF struc_g. + +DATA dref_g TYPE REF TO data. +dref_g = NEW struc_g( comp1 = 1 comp2 = 2 ). + +ASSIGN CAST struc_g( dref_g )->comp1 TO FIELD-SYMBOL(). +ASSIGN CAST struc_g( dref_g )->comp2 TO FIELD-SYMBOL(). +ASSIGN CAST struc_g( dref_g )->* TO FIELD-SYMBOL(). + +DATA iref TYPE REF TO zdemo_abap_objects_interface. +DATA(oref) = NEW zcl_demo_abap_objects( ). +oref->another_string = `hello`. +iref = oref. +iref->in_str = `world`. + +ASSIGN CAST zcl_demo_abap_objects( iref )->another_string TO FIELD-SYMBOL(). +ASSIGN CAST zdemo_abap_objects_interface( oref )->in_str TO FIELD-SYMBOL(). +``` + +

⬆️ back to top