代码之家  ›  专栏  ›  技术社区  ›  Techboy

SAP ABAP中的2列数组A=1,B=2…ZZZ=?

  •  0
  • Techboy  · 技术社区  · 16 年前

    例如:


    B=2个

    ...
    Z=26个
    AA=27个
    AB=28
    ...

    文学学士=

    BZ公司=

    ...

    ZZZ公司=

    请你能建议我如何编码这个。

    有比写数组更好的选择吗?

    4 回复  |  直到 5 年前
        1
  •  4
  •   user235064 user235064    16 年前

    不需要在表中查找值。可计算如下:

    parameters: p_input(3) type c value 'AAA'.
    
    data: len type i value 0,
          multiplier type i value 1,
          result type i value 0,
          idx type i.
    
    * how many characters are there?
    len = strlen( p_input ).
    idx = len.
    
    * compute the value for every char starting at the end
    * in 'ABC' the C is multiplied with 1, the B with 26 and the A with 26^2
    do len times.
    
    * p_input+idx(1) should be the actual character and we look it up in sy-abcde
      search p_input+idx(1) in SY-ABCDE.
    
    * if p_input+idx(1) was A then sy-fdpos should now be set to 0 that is s why we add 1
      compute result = result + ( sy-fdpos + 1 ) * multiplier.
    
      idx = idx - 1.
      multiplier = multiplier * 26.
    enddo.
    
    write: / result.
    

    我没有测试这个程序,它肯定有一些语法错误。但它背后的算法应该是可行的。

        2
  •  1
  •   wise    16 年前

    也许我误解了,但你不想要这样的东西吗?

    type: begin of t_lookup,
            rec_key type string,
            value type i,
          end of t_lookup.
    
    data: it_lookup type hashed table of t_lookup with unique key rec_key.
    

    read table it_lookup with key rec_key = [value] assigning <s>.
    
    if sy-subrc eq 0.
        " got something
    else.
       " didn't
    endif.
    

    不幸的是,ABAP中不存在数组,但是哈希表是为这种查找(快速访问、唯一键)而设计的。

        3
  •  1
  •   Martijn Pieters    13 年前
    DATA: STR TYPE STRING, I TYPE I, J TYPE I, K TYPE I, CH TYPE C, RES
    TYPE INT2, FLAG TYPE I.
    
    PARAMETERS: S(3).
    
    START-OF-SELECTION.
    
      I = STRLEN( S ).
      STR = S.
      DO I TIMES.
        I = I - 1.
        CH = S.
        IF CH CO '1234567890.' OR CH CN SY-ABCDE.
          FLAG = 0.
          EXIT.
        ELSE.
          FLAG = 1.
        ENDIF.
    
        SEARCH SY-ABCDE FOR CH.
    
        J = I.
        K = 1.
        WHILE J > 0.
          K = K * 26.
          J = J - 1.
        ENDWHILE.
        K = K * ( SY-FDPOS + 1 ).
    
        RES = RES + K.
    
        REPLACE SUBSTRING CH IN S WITH ''.
    
      ENDDO.
    *  RES = RES + SY-FDPOS.
    
      IF FLAG = 0.
        MESSAGE 'String is not valid.' TYPE 'S'.
      ELSE.
        WRITE: /, RES  .
      ENDIF.
    

    执行后使用此代码。

        4
  •  0
  •   Neo xcavan    11 年前

    看看这个它对你有用。

          DATA:
      lv_char                TYPE char1,
      lv_len                 TYPE i,
      lv_len_minus_1         TYPE i,
      lv_partial_index1      TYPE i,
      lv_partial_index2      TYPE i,
      lv_number              TYPE i,
      result_tab             TYPE match_result_tab,
      lv_col_index_substr    TYPE string,
      lv_result              TYPE i.
    
      FIELD-SYMBOLS:
                     <match> LIKE LINE OF result_tab.
    
      lv_len = strlen( iv_col_index ) .
      lv_char = iv_col_index(1).
    
      FIND FIRST OCCURRENCE OF lv_char IN co_char RESULTS result_tab.
    
      READ TABLE result_tab ASSIGNING <match> INDEX 1.
      lv_number = <match>-offset .
      lv_number = lv_number + 1 .
    
    
      IF lv_len EQ 1.
        ev_col = ( ( 26 ** ( lv_len - 1 ) ) * lv_number )  .
      ELSE.
        lv_len_minus_1 = lv_len - 1.
        lv_col_index_substr = iv_col_index+1(lv_len_minus_1) .
        CALL METHOD get_col_index
          EXPORTING
            iv_col_index = lv_col_index_substr
          IMPORTING
            ev_col       = lv_partial_index2.
    
        lv_partial_index1 = ( ( 26 ** ( lv_len - 1 ) ) * lv_number ) + lv_partial_index2 .
        ev_col =  lv_partial_index1 .
    
      ENDIF.
    

    这里的算法使用递归逻辑来确定数字列索引。 这不是我的算法,但已经适应在ABAP中使用。

    原来的算法是在openexcel中使用的,现在找不到任何链接。

    推荐文章