0

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.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Techboy
  • 4,286
  • 5
  • 36
  • 45
  • This recent code golf will show you many ways to do it (albeit in several languages you don't care about...) http://stackoverflow.com/questions/2634427/code-golf-numeric-equivalent-of-an-excel-column-name/2634463#2634463 – Mark Rushakoff Apr 22 '10 at 11:01

4 Answers4

4

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.

1

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).

wise
  • 103
  • 1
  • 1
  • 5
  • 1
    You have to use READ TABLE ... WITH TABLE KEY ... instead of ... WITH KEY ... because the latter always performs a linear search. – vwegert Apr 22 '10 at 18:26
  • 1
    thanks, vwgert, always wondered what the difference was. but i see from the next answer i did misunderstand the question anyway. – wise Apr 23 '10 at 03:55
  • +1 for still providing the only "representation" of the array concept in ABAP – Esti May 03 '10 at 01:39
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.

Use this code after executing.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

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.

Neo
  • 29
  • 1
  • 8