This blog is ideal for SAP learners and developers looking to grasp essential ABAP concepts, improve coding efficiency, and prepare for real-world scenarios.
REPORT z_abap101_056. TYPES: BEGIN OF work_area, str TYPE string, date TYPE d, time TYPE t, integer TYPE i, hex TYPE x LENGTH 8, END OF work_area. START-OF-SELECTION. work_area-str = 'This is a string'. work_area-date = '20141225'. " Christmas work_area-time = '134059'. work_area-integer = 101. work_area-hex = '0123456789ABCDEF'. * Before refactoring * WRITE work_area-str. * ULINE. * WRITE work_area-date DD/MM/YY. * ULINE. * WRITE work_area-time. * ULINE. * WRITE work_area-integer. * ULINE. * WRITE work_area-hex. * ULINE. * After refactoring WRITE work_area-str. PERFORM separe_line. WRITE work_area-date DD/MM/YY. PERFORM separe_line. WRITE work_area-time. PERFORM separe_line. WRITE work_area-integer. PERFORM separe_line. WRITE work_area-hex. PERFORM separe_line. *&---------------------------------------------------------------------* *& Form separe_line *&---------------------------------------------------------------------* * Separe each output line *----------------------------------------------------------------------* FORM separe_line. DO 2 TIMES. ULINE. ENDDO. ENDFORM. "separe_line
REPORT z_abap101_057. TYPES: BEGIN OF ty_work_area, str TYPE string, date TYPE d, time TYPE t, integer TYPE i, hex TYPE x LENGTH 8, END OF ty_work_area. DATA work_area TYPE ty_work_area. *&---------------------------------------------------------------------* *& Form count_initial_components *&---------------------------------------------------------------------* * Gets a work area, counts how many components are initial and write * the result *----------------------------------------------------------------------* * -->US_WORK_AREA work area *----------------------------------------------------------------------* FORM count_initial_components USING us_work_area TYPE ty_work_area. DATA lv_initial_components_counter TYPE i. IF us_work_area-str IS INITIAL. lv_initial_components_counter = lv_initial_components_counter + 1. ENDIF. IF us_work_area-date IS INITIAL. lv_initial_components_counter = lv_initial_components_counter + 1. ENDIF. IF us_work_area-time IS INITIAL. lv_initial_components_counter = lv_initial_components_counter + 1. ENDIF. IF us_work_area-integer IS INITIAL. lv_initial_components_counter = lv_initial_components_counter + 1. ENDIF. IF us_work_area-hex IS INITIAL. lv_initial_components_counter = lv_initial_components_counter + 1. ENDIF. WRITE: 'Initial components: ', lv_initial_components_counter. NEW-LINE. ENDFORM. "count_initial_components START-OF-SELECTION. PERFORM count_initial_components USING work_area. work_area-str = 'This is a string'. PERFORM count_initial_components USING work_area. work_area-date = '20141225'. " Christmas PERFORM count_initial_components USING work_area. work_area-time = '134059'. PERFORM count_initial_components USING work_area. work_area-integer = 101. PERFORM count_initial_components USING work_area. work_area-hex = '0123456789ABCDEF'. PERFORM count_initial_components USING work_area.
REPORT z_abap101_058. TYPES: BEGIN OF ty_work_area, integer TYPE i, float TYPE f, pack TYPE p LENGTH 8 DECIMALS 3, decfloat34 TYPE decfloat34, END OF ty_work_area. *&---------------------------------------------------------------------* *& Form sum_numeric_components *&---------------------------------------------------------------------* * Receives a work area with numeric components and sum them. *----------------------------------------------------------------------* * -->US_WA Work area with numeric components *----------------------------------------------------------------------* FORM sum_numeric_components USING us_wa TYPE ty_work_area. DATA lv_sum_result TYPE decfloat34. lv_sum_result = us_wa-integer + us_wa-float + us_wa-pack + us_wa-decfloat34. WRITE lv_sum_result. NEW-LINE. ENDFORM. "sum_numeric_components START-OF-SELECTION. DATA work_area TYPE ty_work_area. DATA work_area_doubled TYPE ty_work_area. work_area-integer = 2. work_area-float = '2.5'. work_area-pack = '2.12345'. work_area-decfloat34 = 1000000000000000000000000000000. PERFORM sum_numeric_components USING work_area. work_area_doubled-integer = work_area-integer * 2. work_area_doubled-float = work_area-float * 2. work_area_doubled-pack = work_area-pack * 2. work_area_doubled-decfloat34 = work_area-decfloat34 * 2. PERFORM sum_numeric_components USING work_area_doubled.
REPORT z_abap101_059. TYPES: BEGIN OF ty_char_and_numeric, char_comp1 TYPE string, char_comp2 TYPE c LENGTH 3, char_comp3 TYPE n LENGTH 10, num_comp1 TYPE i, num_comp2 TYPE f, num_comp3 TYPE decfloat16, END OF ty_char_and_numeric. *&---------------------------------------------------------------------* *& Form clear_char_or_numeric *&---------------------------------------------------------------------* * This routine clears some component values according to the following rules: * a. Clear char components only if the sum of the numeric components is odd * (ignoring possible decimal places) * b. Clear numeric components only if the sum of vowels in the three char * components is even (ignoring lower/upper case) *----------------------------------------------------------------------* * -->US_WA_CHAR_AND_NUMERIC text *----------------------------------------------------------------------* FORM clear_char_or_numeric USING us_wa_char_and_numeric TYPE ty_char_and_numeric. DATA lv_mod_result TYPE i. DATA lv_sum_numeric TYPE i. lv_sum_numeric = us_wa_char_and_numeric-num_comp1 + us_wa_char_and_numeric-num_comp2 + us_wa_char_and_numeric-num_comp3. lv_mod_result = lv_sum_numeric MOD 2. IF lv_mod_result <> 0. CLEAR: us_wa_char_and_numeric-char_comp1, us_wa_char_and_numeric-char_comp2, us_wa_char_and_numeric-char_comp3. RETURN. ENDIF. DATA lv_vowel_count TYPE i. DATA lv_current_vowel_count TYPE i. FIND ALL OCCURRENCES OF REGEX 'a|e|i|o|u|A|E|I|O|U' IN us_wa_char_and_numeric-char_comp1 MATCH COUNT lv_current_vowel_count. lv_vowel_count = lv_vowel_count + lv_current_vowel_count. FIND ALL OCCURRENCES OF REGEX 'a|e|i|o|u|A|E|I|O|U' IN us_wa_char_and_numeric-char_comp2 MATCH COUNT lv_current_vowel_count. lv_vowel_count = lv_vowel_count + lv_current_vowel_count. FIND ALL OCCURRENCES OF REGEX 'a|e|i|o|u|A|E|I|O|U' IN us_wa_char_and_numeric-char_comp3 MATCH COUNT lv_current_vowel_count. lv_vowel_count = lv_vowel_count + lv_current_vowel_count. lv_mod_result = lv_vowel_count MOD 2. IF lv_mod_result = 0. CLEAR: us_wa_char_and_numeric-num_comp1, us_wa_char_and_numeric-num_comp2, us_wa_char_and_numeric-num_comp3. RETURN. ENDIF. ENDFORM. "clear_char_or_numeric START-OF-SELECTION. DATA wa_char_cleared TYPE ty_char_and_numeric. DATA wa_numeric_cleared TYPE ty_char_and_numeric. wa_char_cleared-char_comp1 = 'This should be clea'. wa_char_cleared-char_comp2 = 'red'. wa_char_cleared-char_comp3 = '0123456789'. wa_char_cleared-num_comp1 = 1. wa_char_cleared-num_comp2 = 10. wa_char_cleared-num_comp3 = 100. WRITE: wa_char_cleared-char_comp1, wa_char_cleared-char_comp2, wa_char_cleared-char_comp3, wa_char_cleared-num_comp1, wa_char_cleared-num_comp2, wa_char_cleared-num_comp3. NEW-LINE. PERFORM clear_char_or_numeric USING wa_char_cleared. WRITE: wa_char_cleared-char_comp1, wa_char_cleared-char_comp2, wa_char_cleared-char_comp3, wa_char_cleared-num_comp1, wa_char_cleared-num_comp2, wa_char_cleared-num_comp3. NEW-LINE. ULINE. wa_numeric_cleared-char_comp1 = 'aeiouAEIOU'. wa_numeric_cleared-char_comp2 = 'BCD'. wa_numeric_cleared-char_comp3 = '0123456789'. wa_numeric_cleared-num_comp1 = 2. " even wa_numeric_cleared-num_comp2 = 10. wa_numeric_cleared-num_comp3 = 100. WRITE: wa_numeric_cleared-char_comp1, wa_numeric_cleared-char_comp2, wa_numeric_cleared-char_comp3, wa_numeric_cleared-num_comp1, wa_numeric_cleared-num_comp2, wa_numeric_cleared-num_comp3. NEW-LINE. PERFORM clear_char_or_numeric USING wa_numeric_cleared. WRITE: wa_numeric_cleared-char_comp1, wa_numeric_cleared-char_comp2, wa_numeric_cleared-char_comp3, wa_numeric_cleared-num_comp1, wa_numeric_cleared-num_comp2, wa_numeric_cleared-num_comp3. NEW-LINE.
REPORT z_abap101_060. TYPES: BEGIN OF ty_line, id TYPE c LENGTH 10, name TYPE string, value TYPE i, END OF ty_line. DATA it_standard TYPE STANDARD TABLE OF ty_line. DATA it_sorted TYPE SORTED TABLE OF ty_line WITH UNIQUE KEY id. DATA it_hashed TYPE HASHED TABLE OF ty_line WITH UNIQUE KEY id. START-OF-SELECTION. DATA wa TYPE ty_line. wa-id = '3'. wa-name = 'John'. wa-value = 50. APPEND wa TO it_standard. INSERT wa INTO TABLE it_sorted. INSERT wa INTO TABLE it_hashed. wa-id = '2'. wa-name = 'Mary'. wa-value = 60. APPEND wa TO it_standard. INSERT wa INTO TABLE it_sorted. INSERT wa INTO TABLE it_hashed. wa-id = '1'. wa-name = 'Max'. wa-value = 30. APPEND wa TO it_standard. INSERT wa INTO TABLE it_sorted. INSERT wa INTO TABLE it_hashed. BREAK-POINT.
No comments:
Post a Comment