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

时间:2010-04-22 10:50:28

标签: sap abap

我需要在ABAP中创建一个2列数组,以便程序可以查找记录项(由字母A - ZZZ定义),然后返回与之关联的数字。

例如:

A = 1
B = 2
C = 3
...... Z = 26
AA = 27
AB = 28
...... AZ =
BA =
...... BZ =
CA =
...... ...... ZZZ =

请您建议我如何编码。

有没有比编写数组更好的选择?

感谢。

4 个答案:

答案 0 :(得分:4)

您不需要在表中查找值。这可以计算出来:

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.

我没有测试程序,它确实有一些语法错误。但它背后的算法应该有效。

答案 1 :(得分:1)

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

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中不存在数组,但是为这种查找设计了散列表(快速访问,唯一键)。

答案 2 :(得分:1)

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.

执行后使用此代码。

答案 3 :(得分:0)

我在一段时间后做了类似的实施。 检查它是否适合你。

      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中使用。

原始算法用于Open Excel,现在无法找到任何链接。