如何定义 2 列数组 A = 1, B = 2...ZZZ =?

发布于 2024-08-29 15:56:20 字数 306 浏览 5 评论 0原文

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

例如:

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

请您建议我如何编码。

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

谢谢。

I need to create a 2 column array in ABAP so that a program can look up a record item (defined by the letters A - ZZZ) and then return the number associated with it.

For example:

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

Please can you suggest how I can code this.

Is there a better option than writing an array?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半世蒼涼 2024-09-05 15:56:20

您不需要查找表中的值。这是可以计算的:

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.

我没有测试该程序,并且它非常肯定有一些语法错误。但其背后的算法应该有效。

you don't need to lookup the value in a table. this can be calculated:

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.

i didn't test the program and it has pretty sure some syntax errors. but the algorithm behind it should work.

紫轩蝶泪 2024-09-05 15:56:20

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

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

perhaps I'm misunderstanding, but don't you want something like this?

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.

then once it's populated, read it back

read table it_lookup with key rec_key = [value] assigning <s>.

if sy-subrc eq 0.
    " got something
else.
   " didn't
endif.

unfortunately, arrays don't exist in ABAP, but a hashed table is designed for this kind of lookup (fast access, unique keys).

时光病人 2024-09-05 15:56:20
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.

执行后使用此代码。

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.

Use this code after executing.

两相知 2024-09-05 15:56:20

我不久前也做过类似的实现。
检查一下它是否适合您。

      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中使用的,现在找不到任何链接。

I did a similar implementation some time back.
Check this it it works for you.

      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.

Here The algorithm uses a recursive logic to determine the column index in numbers.
This is not my algorithm but have adapted to be used in ABAP.

The original algorithm is used in Open Excel, cant find any links right now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文