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 Question

Former Member
0 Likes
761

I have an internal table that I loop through using a WHERE statement. I want to be able to tell if its the last run through the loop. If it is, I want to do some additional code. Currently, I loop through it one time to count the number of times, then I loop again for main functionality. Is there a better way to do this?

Here is what I currently do:

mycount = 0.
LOOP AT mytable INTO mywa WHERE kunnr = currentkunnr.
     mycount = mycount + 1.
ENDLOOP.

LOOP AT mytable INTO mywa WHERE kunnr = currentkunnr.
     IF sy-tabix = mycount.
          <CODE>
     ENDIF.
     <CODE>
ENDLOOP.

Thanks,

Curtis

7 REPLIES 7
Read only

Former Member
0 Likes
730

try using At Last.

Loop.

code....

At Last.

Code exclusively tobe done at last one

endat

Endloop.

Read only

0 Likes
730

The AT LAST statement will not work when I have a condition on there loop. Are there any other suggestions?

Read only

0 Likes
730

Hi Davis, Try this way.


REPORT  ztest_notepad.
DATA: BEGIN OF it_nfal OCCURS 0,
        einri TYPE nfal-einri,
        falnr TYPE nfal-falnr,
        falar TYPE nfal-falar,
        patnr TYPE nfal-patnr,
      END OF it_nfal.

START-OF-SELECTION.
  SELECT *
  FROM nfal
  INTO CORRESPONDING FIELDS OF TABLE it_nfal
  UP TO 10000 ROWS.
  "Sort internal table by key field which is used in Where condition.  
  SORT it_nfal BY einri.
  LOOP AT it_nfal WHERE einri = 'CGH'.
    "It is triggered when it reaches last value.
    AT END OF einri.
      WRITE 'Test this'.
    ENDAT.
  ENDLOOP.
Thanks Venkat.O

Read only

Former Member
0 Likes
730

Hi ,

Loop at itab.

-


-


at last.

-


-


endat.

endloop.

at last is the event which is exclusively used for the last record.

Hope this will help you.

Thanks,

Rajesh.

Read only

Former Member
0 Likes
730

You could find out the number of lines in the table first.

DATA: I_TABLE LIKE ZTABLE OCCURS 0 WITH HEADER LINE,

V_LINES TYPE I.

DESCRIBE TABLE I_TABLE LINES V_LINES.

LOOP AT I_TABLE WHERE VALUE = CONDITION.

IF SY-TABIX = V_LINES.

  • ADDITIONAL CODE HERE

ENDIF.

ENDLOOP.

Read only

Former Member
0 Likes
730

Try AT END <Field>

This will compare the value of your current record with your next record in the itab. Then, if the next record value in that field is different, this AT END will trigger. Insert your coding here.

Hope this is what you are looking for.

Thanks

Kiran

Read only

faisalatsap
Active Contributor
0 Likes
730

Hi, Davis

Please use Meaningful Subject Line for next time, Please

Regards,

Faisal