Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Reset index of internal table.

Former Member
0 Kudos
960

Hi,

I have a problem in resetting the index of internal table.

my table has five records.

once i reach third record i am checking for a condition , if that condition is successful i am executing a code.

once i am done with the execution i want to loop the internal table again from third record.

even if the change the sy-tabix to 3 , my internal table loop is reading fourth record .

can you let me know how to resolve this .

Regards,

Roby

6 REPLIES 6

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos
230

Roby, your requirement is not 100% clear, but do you only want to read that one line after your condition code, try reading directly with READ statement before continuing on in the LOOP.

Read table itab into wa index sy-tabix.

Regards,

Rich Heilman

0 Kudos
230

Rich,

i want to continue the loop from third record .

Roby

0 Kudos
230

Hi Roby,

you can use the above logic that mentioned.

That is...

LOOP AT ITAB FROM 3.

ENDLOOP.

Thanks,

Ramakrishna

0 Kudos
230

You might get into trouble with an infinite loop here (it'll always be the third record). You could:


loop at itab.
  if sy-tabix = 3.
*   process.
  exit.
endloop.

loop at itab from 3.
* do the other processing.
endloop.

Rob

venkata_ramisetti
Active Contributor
0 Kudos
230

Hi Roby,

if you set SY-TABIX inside the loop, it will not work in the next loop iteration.

If you want to read from the third record again, you can write logi like below...

LOOP AT ITAB FROM 3.

ENDLOOP.

Thanks,

Ramakrishna

Former Member
0 Kudos
230

Hi Roby,

Another solution would look like this:

* Local
  TYPES:
    BEGIN OF local_record_type,
      field1 TYPE syindex,
      field2 TYPE syindex,
    END OF local_record_type.
  DATA:
    number_of_records TYPE i,
    local_workarea    TYPE local_record_type,
    local_table       TYPE STANDARD TABLE OF local_record_type.

* Test fill (demo purposes)
  DO 10 TIMES.
    local_workarea-field1 = sy-index.
    local_workarea-field2 = ( 10 - sy-index ).
    APPEND local_workarea TO local_table.
  ENDDO.
* Your proces starts here
  DESCRIBE TABLE local_table LINES number_of_records.
* Controlled process
  DO number_of_records TIMES.
    CHECK:
      sy-index GE 2.
    IF sy-index EQ 3.   
      READ TABLE local_table INDEX sy-index INTO local_workarea.
*     Your action
*     ...
    ENDIF.
*   After sy-index reaches 3 and the proces has taken place
*   sy-index becomes 4 and you can READ the next record as above
*   and do your stuff with it.
*   ...
  ENDDO.

Regards,

Rob.