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

Looping through Objects

Former Member
0 Likes
4,150

Hi,

I'm trying to design business entities with ABAP Objects. I have been able to create internal tables of custom object types.

I stumbled into a very peculiar situation in which i have to loop through my custom object internal table. i couldn't use the WHERE specification since the line type is not a structure. READ TABLE doesnt work either. What i did to overcome the problem was to do a LOOP AT with an IF statement inside and an EXIT command to quit the search when found.

Is there a better solution? And Is the whole idea of wrapping everything in classes and accessing the through an internal idea a good idea in the first place?

Thanks.

11 REPLIES 11
Read only

athavanraja
Active Contributor
2,638

couldnt really understand the question. hereis what i understand.

<i>i have to loop through my custom object internal table</i>

whats this object (a class?) and itab is a attribute of the class?

amy be post the piece of code your are using, we can see whether we can overcome the issue you are talking about.

Regards

Raja

Read only

0 Likes
2,638

heres an example:

* DEFINITIONS
* -----------
CLASS cl_drag_drop_picture DEFINITION INHERITING FROM cl_gui_picture.
  PUBLIC SECTION.
    DATA: row     TYPE I,
          col     TYPE I.

DATA: g_wa_pic_ctrl TYPE REF TO cl_drag_drop_picture.

DATA: g_it_pic_ctrl LIKE TABLE OF g_wa_pic_ctrl.

* PROCESS
* -------

* lets assume that g_it_pic_ctrl has several entries and
* each entry is uniquely identified by the attributes
* row AND col.
PERFORM GetObjectByRowCol USING p_row
                                p_col
                       CHANGING g_wa_pic_ctrl.

* SUBROUTINES
* -----------
FORM GetObjectByRowCol USING p_row
                             p_col
                    CHANGING r_pic_ctrl.

  DATA: l_wa_pic_ctrl LIKE g_wa_pic_ctrl.

* Search for picture
  LOOP AT g_it_pic_ctrl INTO l_wa_pic_ctrl.

    IF l_wa_pic_ctrl->row EQ p_row AND
       l_wa_pic_ctrl->col EQ p_col.
      r_pic_ctrl = l_wa_pic_ctrl.
      EXIT.
    ENDIF.

  ENDLOOP.

* Collect Garbage
  CLEAR l_wa_pic_ctrl.

ENDFORM.

What I couldn't do is access my internal table like:

READ TABLE g_it_pic_ctrl 
      INTO r_pic_ctrl 
  WITH KEY row = p_row
           col = p_col.

OR

LOOP AT g_it_pic_ctrl INTO r_pic_ctrl 
                     WHERE row EQ p_row
                           col EQ p_col.

Read only

0 Likes
2,638

Hi

these two stataments:

READ TABLE g_it_pic_ctrl INTO r_pic_ctrl

WITH KEY row = p_row col = p_col.

OR

LOOP AT g_it_pic_ctrl INTO r_pic_ctrl

WHERE row EQ p_row

col EQ p_col.

EXIT.

ENDLOOP.

give the same result, only difference is in performance.

This statament

LOOP AT g_it_pic_ctrl INTO r_pic_ctrl

WHERE row EQ p_row

col EQ p_col.

ENDLOOP.

gives the last record satisfies the where condition, so you give us more details to say you which statament you should use.

Max

Read only

0 Likes
2,638

Hi, thanks for the quick response..

if we were dealing with a normal internal table, yes, it would only be a question of performance. However, we're using a class as a linetype so READ TABLE and LOOP AT WHERE wouldn't work since both row and col are not components of structure but rather attributes of a class.

The LOOP AT.. IF. works just fine.

My question is actually a 'best practice' question rather than an issue... Is there a better way? or this shouldnt be done at all?

Read only

0 Likes
2,638

Hi

OK! Now I've understood what you want...

I Think there isn't a better way, it needs to check all objects (of internal tables), so only LOOP can be used and an exit condition is placed into the same loop.

Anyway you can try to use the READ stataments:

DO.

READ TABLE G_IT_PIC_CTRL INTO G_WA_PIC_CTRL INDEX SY-INDEX.

IF SY-SUBRC <> 0. CLEAR G_WA_PIC_CTRL. EXIT. ENDIF.

IF G_WA_PIC_CTRL->ROW = ROW AND

G_WA_PIC_CTRL->COL = COL.

EXIT.

ENDIF.

ENDDO

Max

Read only

0 Likes
2,638

> I Think there isn't a better way,

aw.. that's sad. I just realized i couldn't use binary searches on these cases.. unless of course i write my own search algos... arr.. thanks anyways!

Read only

0 Likes
2,638

Sorry for the delay.

It is possible to use where class in the loop for objects.

check out the following code sample.

data: alv type ref to CL_GUI_FRONTEND_SERVICES .
data: begin of alv_tab occurs 0 ,
      alv_line type ref to CL_GUI_FRONTEND_SERVICES ,
      end of alv_tab .
data: wa like line of alv_tab .
do 10 times .
clear alv .
create object alv .
wa-alv_line =  alv .
append wa to alv_tab .
enddo .

loop at alv_tab into wa where alv_line->ACTIVEX = 'X'.

endloop .

Regards

Raja

Read only

0 Likes
2,638

Hi Raja

You're right, I didn't try your solution, so what depends on how the table is declared:

CLASS CL_DRAG_DROP_PICTURE DEFINITION INHERITING FROM CL_GUI_PICTURE.

PUBLIC SECTION.

DATA: ROW TYPE I,

COL TYPE I.

DATA: BEGIN OF G_WA_PIC_CTRL,

CTRL_PICTYPE TYPE REF TO CL_DRAG_DROP_PICTURE,

END OF G_WA_PIC_CTRL.

DATA: G_IT_PIC_CTRL LIKE TABLE OF G_WA_PIC_CTRL.

METHODS: READ.

ENDCLASS.

CLASS CL_DRAG_DROP_PICTURE IMPLEMENTATION.

METHOD READ.

LOOP AT G_IT_PIC_CTRL INTO G_WA_PIC_CTRL

WHERE CTRL_PICTYPE->ROW = P_ROW

AND CTRL_PICTYPE->COL = P_COL.

ENDLOOP.

ENDMETHOD. "READ

ENDCLASS.

Max

Read only

0 Likes
2,638

great stuff! hmmm.. i guess i'll have to redesign. Thanks!

Read only

0 Likes
2,638

can you mark the thread as answered.

Regards

Raja

Read only

Markus_Ebel
Explorer
2,638

It is the same as with standard internal tables where you have no dedicated fields (for example: itab type standard table of string). For that you can refer to the current line with the keyword "table_line", e.g.

LOOP AT g_it_pic_ctrl INTO l_wa_pic_ctrl.

WHERE table_line->ROW = p_row

   AND    table_line->COL = p.col.