I saw this snippet in ABAP Development discussion. The code was given by Jānis B

I am posting it here as the original discussion is now rejected, probably because of being too basic.

This snippet uses a fancy combination of functional method call and singleton pattern to build range for DELETE..WHERE field IN <range> statement.

  1. *———————————————————————-*
  2. *       CLASS zcl_pattern_range DEFINITION
  3. *———————————————————————-*
  4. *
  5. *———————————————————————-*
  6. CLASS zcl_pattern_range DEFINITION
  7.   FINAL
  8.   CREATE PRIVATE .
  9.   PUBLIC SECTION.
  10.     TYPES: rt_patterns TYPE RANGE OF string .
  11.     TYPES: rs_pattern TYPE LINE OF rt_patterns .
  12.     CLASS-METHODS get_instance
  13.       RETURNING value(ro_instance) TYPE REF TO zcl_pattern_range.
  14.     METHODS:
  15.      get_exclude_rng
  16.        RETURNING value(rt_exclude_patterns) TYPE rt_patterns ,
  17.      get_include_rng
  18.        RETURNING value(rt_include_patterns) TYPE rt_patterns .
  19.   PROTECTED SECTION.
  20.   PRIVATE SECTION.
  21.     CLASS-DATA mo_instance TYPE REF TO zcl_pattern_range.
  22.     METHODS get_range
  23.         IMPORTING i_sign TYPE c
  24.         RETURNING value(rt_patterns) TYPE rt_patterns .
  25. ENDCLASS.                    “zcl_pattern_range DEFINITION
  26. *———————————————————————-*
  27. *       CLASS zcl_pattern_range IMPLEMENTATION
  28. *———————————————————————-*
  29. *
  30. *———————————————————————-*
  31. CLASS zcl_pattern_range IMPLEMENTATION.
  32.   METHOD get_instance.
  33.     IF mo_instance IS NOT BOUND .
  34.       CREATE OBJECT mo_instance .
  35.     ENDIF .
  36.     ro_instance = mo_instance .
  37.   ENDMETHOD.                    “get_instance
  38.   METHOD get_range.
  39.     DATA: ls_pattern TYPE rs_pattern .
  40.     DATA: l_number TYPE i .
  41.     REFRESH rt_patterns .
  42.     ls_patternsign = i_sign .
  43.     ls_patternoption = ‘CP’ .
  44.     WHILE l_number LE 9 .
  45.       ls_patternlow = l_number && ‘*’ .
  46. *      ls_pattern-low = |{ l_number }| && ‘*’ .
  47.       APPEND ls_pattern TO rt_patterns .
  48.       ADD 1 TO l_number.
  49.     ENDWHILE .
  50.   ENDMETHOD.                    “get_range
  51.   METHOD get_exclude_rng .
  52.     REFRESH rt_exclude_patterns .
  53.     rt_exclude_patterns = me->get_range( ‘E’ ).
  54.   ENDMETHOD .                    “get_exclude_rng
  55.   METHOD get_include_rng .
  56.     REFRESH rt_include_patterns .
  57.     rt_include_patterns = me->get_range( ‘I’ ).
  58.   ENDMETHOD .                    “get_include_rng
  59. ENDCLASS.                    “zcl_pattern_range IMPLEMENTATION
  60. TYPES: BEGIN OF ts_string,
  61.         string TYPE string,
  62.       END OF ts_string .
  63. DATA: gs_string TYPE ts_string .
  64. DATA: gt_string TYPE STANDARD TABLE OF ts_string .
  65. START-OF-SELECTION .
  66.   REFRESH gt_string .
  67.   gs_stringstring = ‘a12344’. APPEND gs_string TO gt_string.
  68.   gs_stringstring = ‘1234vb’. APPEND gs_string TO gt_string.
  69.   gs_stringstring = ‘c42456d44’. APPEND gs_string TO gt_string .
  70.   DELETE gt_string WHERE
  71.     string IN zcl_pattern_range=>get_instance( )->get_exclude_rng( ).
  72.   BREAK-POINT .
  73.   REFRESH gt_string .
  74.   gs_stringstring = ‘a12344’. APPEND gs_string TO gt_string.
  75.   gs_stringstring = ‘1234vb’. APPEND gs_string TO gt_string.
  76.   gs_stringstring = ‘c42456d44’. APPEND gs_string TO gt_string .
  77.   DELETE gt_string WHERE
  78.     string NOT IN zcl_pattern_range=>get_instance( )->get_include_rng( ).

/.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply