tags:

views:

253

answers:

2

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.

+1  A: 

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
You have to use READ TABLE ... WITH TABLE KEY ... instead of ... WITH KEY ... because the latter always performs a linear search.
vwegert
thanks, vwgert, always wondered what the difference was. but i see from the next answer i did misunderstand the question anyway.
wise
+1 for still providing the only "representation" of the array concept in ABAP
Esti
+3  A: 

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.

flurin
Good logic - many thanks :-)
Techboy
+1 for SY-ABCDE. Didn't know this one...
PATRY