在内部表ABAP中查找不可打印的字符

时间:2013-05-21 16:38:11

标签: sap abap

我有一个内部表,它被写入文件,然后作为数据源被拉入BW。有时,不可打印的字符会使其进入文件输出并将导入过程分解为BW。下面是我的代码示例。由于itab不是类型c或字符串,我无法在其上使用find / replace正则表达式。有没有其他人必须解决这类问题?

    FORM eliminate_non_print_char TABLES p_shiptab STRUCTURE shiptab.     

  LOOP AT p_shiptab INTO wa_shiptab.

    FIND REGEX '[^[:print:]]+(?!$)'
      IN wa_shiptab
    IGNORING CASE.
    "RESULTS result.
    IF  sy-subrc = 0.

     REPLACE REGEX '[^[:print:]]+(?!$)'
        IN wa_shiptab WITH ''
     IGNORING CASE.

    ENDIF.
  ENDLOOP.

DATA: BEGIN OF shiptab OCCURS 2000.
        INCLUDE STRUCTURE ship1.
        INCLUDE STRUCTURE ship2.
DATA: landtx            LIKE vbrk-landtx,
      bl_konwa          LIKE vbak-waerk.                    
        INCLUDE STRUCTURE ship3.
        INCLUDE STRUCTURE ship4.
DATA: frght_amnt_usd    LIKE konv-kwert,                    
      revenue_amnt_usd  LIKE vbap-netwr,                    
      unit_price_usd    LIKE vbap-netpr,                    
      pgi_posting_date  LIKE mkpf-budat,                    
      ord_line_item_qty LIKE lips-lfimg,                    
      asm_no            LIKE kna1-kunnr,
      asm_username      LIKE adrc-sort1,
      va_augru_t        LIKE tvaut-bezei,         
      ship_to_name      LIKE adrc-name1,
      bill_to_name      LIKE adrc-name1,
      forward_to_name   LIKE adrc-name1,
      fmv_amnt           LIKE konv-kbetr,    
      va_butxt           LIKE t001-butxt,    
      sold_to_search_term LIKE adrc-sort1,   
      bill_to_search_term LIKE adrc-sort1,  
      va_prctr           LIKE vbap-prctr,    
      va_bezei           LIKE tvrot-bezei.   
        INCLUDE STRUCTURE zorder_attr.         
DATA: extended_bits_count(20),              
      va_bstkd_hdr LIKE char32.            
DATA: gsm_bp_katr6      LIKE kna1-katr6,    
      gsm_bp_vtext6     LIKE tvk6t-vtext,   
      asm_ze_katr7      LIKE kna1-katr7,    
      asm_ze_vtext7     LIKE tvk7t-vtext,   
      gsm_ze_katr6      LIKE kna1-katr6,    
      gsm_ze_vtext6     LIKE tvk7t-vtext.
DATA:  END OF shiptab

我得到的错误是:“WA_SHIPTAB”必须是字符型数据对象(数据类型C,N,D,T或STRING)。我在itab shiptab中有非字符类型。我知道我可以单独对每个字段执行此查找,但itab有235个字段,但效果似乎不高。

2 个答案:

答案 0 :(得分:2)

该程序有另一个主要的itab副本,它是基于字符的字段,我循环使用并放置以下代码:

REPLACE ALL OCCURRENCES OF REGEX '[^[:print:]]+$'
  IN transtab WITH ''
  IGNORING CASE.

答案的链接:

SCN Link

Help.SAP Link

答案 1 :(得分:1)

您可以使用运行时类型服务来遍历表中每个字段的定义,以确定哪些类型与REGEX操作类型兼容。然后,您可以使用动态分配来仅处理那些可兼容的字段,一次一个。例如:

" Initalize Range of typekinds that are compatable with REGEX. See constant 
" attributes of CL_ABAP_DATADESCR for typekind definitions. You'll have to
" define these explicitly
lr_typekind_regex = <...>. 

" Get components of table structure
lo_tabdescr = cl_abap_typedescr=>describe_by_data( p_shiptab ).
lo_strdescr = lo_tabdescr->get_table_ine_type( ).
li_comp = lo_structdescr->get_components( ).

" Loop through table, sanitizing regex-compatable fields in each row
LOOP AT p_shiptab ASSIGNING <la_shiptab>.
  LOOP AT li_comp INTO la_comp.
    CHECK la_comp-type->type_kind IN lr_typekind_regex.

    " Call subroutine containing Regex to remove non-printable chars from field        
    ASSIGN COMPONENT la_comp-name OF STRUCTURE <la_shiptab> TO <l_charvalue>.
    PERFORM sanitize_field CHANGING <l_charvalue>. 

  ENDLOOP.
ENDLOOP.