Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment an integer

Tags:

abap

Sometimes ABAP drives me crazy with really simple tasks such as incrementing an integer within a loop...

Here's my try:

METHOD test.

  DATA lv_id TYPE integer.

  lv_id = 1.

  LOOP AT x ASSIGNING <y>.
    lv_id = lv_id+1.
  ENDLOOP.

ENDMETHOD.

This results in the error message Field type "I" does not permit subfield access.

like image 659
Ben Avatar asked Jun 02 '10 13:06

Ben


2 Answers

You already answered the question yourself, but to make things a bit clearer:

variable + 1 

is an arithmetic expression - add 1 to the value of the variable.

variable+1

is an offset operation on a character variable. For example, if variable contains ABC, variable+1 is BC.

This can be especially confusing when dealing with NUMCs. For example, with variable = '4711', variable + 1 is evaluated to 4712, whereas variable+1 is '711' (a character sequence).

The error you saw occurred because it's not possible to perform the index operation on a non-character-like variable.

like image 140
vwegert Avatar answered Nov 07 '22 07:11

vwegert


You mean like:

ADD 1 to lv_id.

By the way, when you loop over an internal table, SY-TABIX has the loop counter.

like image 23
tomdemuyt Avatar answered Nov 07 '22 06:11

tomdemuyt