Application Development and Automation 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: 
Read only

Loop using index read ( internal table)

Former Member
0 Likes
7,131

Hi,

I thought of impelementing loop at ITAB using read statements, Here it goes.

REPORT Z_LOOP_IMPROVE.

DATA : T_OUTPUT TYPE STANDARD TABLE OF MARC WITH HEADER LINE,

L_TABIX TYPE SY-TABIX.

DEFINE ILOOP.

DO.

IF SY-INDEX EQ '1'.

READ TABLE &1 WITH KEY &2 = &3 BINARY SEARCH.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

ELSE.

L_TABIX = SY-TABIX + 1.

READ TABLE &1 INDEX L_TABIX.

IF SY-SUBRC NE 0 OR &1-&2 NE &3.

EXIT.

ENDIF.

ENDIF.

END-OF-DEFINITION.

DEFINE IENDLOOP.

ENDDO.

END-OF-DEFINITION.

DATA : T1 TYPE I, T2 TYPE I.

SELECT * FROM MARC INTO TABLE T_OUTPUT.

SORT T_OUTPUT BY WERKS.

GET RUN TIME FIELD T1.

ILOOP T_OUTPUT WERKS '0001'.

WRITE : / T_OUTPUT-MATNR, T_OUTPUT-WERKS.

IENDLOOP.

GET RUN TIME FIELD T2.

T2 = T2 - T1.

WRITE : / T2.

*C----


But sadly it takes more time than a normal loop with

where condition.

can anyone suggest some ideas to improve execution speed?

1 ACCEPTED SOLUTION
Read only

christian_wohlfahrt
Active Contributor
0 Likes
1,763

Hi!

As long as a sorted table is out of scope, that is the correct way.

But maybe the macro-handling adds more time then simple loop where.

Instead of second read, you can also use a loop at itab from sy-index (in explicit programming). Personally I would avoid macros anyway, but making a function module will need extra time, too.

Regards,

Christian

6 REPLIES 6
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,763

Just LOOP at the internal table. Its doing the DO READ behind the LOOP anyway.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,763

You can use a binary read to get the first record. Then read each record sequentially until you have proccessed all the records that meet your critera. You will have to have the internal table sorted so that the binary search and subsequent reads will work:


      READ TABLE itab WITH KEY
        field = whatever
        BINARY SEARCH.
      IF sy-subrc = 0.
        itab_index = sy-tabix.
        DO.
          IF sy-subrc = 0.
            IF itab_data-field = whatever
              itab_index = itab_index + 1.
              READ TABLE itab_data INDEX itab_index.
            ELSE.
              EXIT.
            ENDIF.
          ELSE.
            EXIT.
          ENDIF.
        ENDDO.
      ENDIF.

Rob

Read only

0 Likes
1,763

Note that this is useful only if both tables are large; otherwise, it's not worth the extra programming aggravation.

Rob

Read only

christian_wohlfahrt
Active Contributor
0 Likes
1,764

Hi!

As long as a sorted table is out of scope, that is the correct way.

But maybe the macro-handling adds more time then simple loop where.

Instead of second read, you can also use a loop at itab from sy-index (in explicit programming). Personally I would avoid macros anyway, but making a function module will need extra time, too.

Regards,

Christian

Read only

Former Member
0 Likes
1,763

instead directly use loop...endloop & read statements with index.

loop at <inttable>.

read <inttable2> index <integer>.

endloop.

Read only

Former Member
0 Likes
1,763

Guys, thanks a lot for ur response.