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

Coding in Badi

Former Member
0 Likes
669

Hi,

Please see the coding... My requirement is i have to compare Item_text field with all the records in loop. Below condition is satisfying only if two records are there. i.e. it is only containging the first record text. how to overcome this ?

LOOP AT lt_item INTO ls_item

IF lv_item_text IS INITIAL

MOVE ls_item-item_text TO lv_item_text.

CONTINUE.

ENDIF.

IF ls_item-item_text = lv_item_text.

--- --

- ---

ENDIF.

ENDLOOP.

Regards

Reddy

5 REPLIES 5
Read only

Former Member
0 Likes
637

Hi Reddy,

Your code is only going to compare on the first record because after the first time through your loop the field lv_item_text is no longer initial. If you want to move ls_item-item_text on every loop, then you need to clear the field lv_item_text just before your ENDLOOP statement. I hope this helps.

- April King

Read only

0 Likes
637

See i have to check lv_item_text with all the records in the same loop. after completing this i have go to the second record and move the ls_item-text to lv_item_text and again have to compare all the records in table like this for all the records i have to do.

Read only

0 Likes
637

I not sure what you exactly want but the below might help

loop at lv_item into ls_item.

loop at lv_item into l_item where

text = lv_item_text.

*what you wish to do

endloop.

endloop.

Read only

0 Likes
637

Right. But you aren't moving ls_item-text to lv_item_text after the first loop because the condition "IF lv_item_text IS INITIAL" is false after the first loop (you've already moved ls_item-text into that field in your first loop). So after you've done your comparisons in the loop you need to clear lv_item_text if you want that field to be initial for the next run through the loop. Is that a little clearer? Or you can take out the "IF lv_item_text IS INITIAL" condition so that the field will automatically be moved every time through the loop.

Or do you mean that you need to compare all of the records in lt_item with lv_item_text (which is from whatever record in lt_item you're currently on)? Sorry, I'm trying to make sure I'm interpreting your question correctly. If this is what you mean, then you basically want to do nested loops through the same table. I don't think you can actually do this. As long as lt_item isn't too big, I would suggest copying it into another table and changing your code to something like this:

la_item[] = lt_item[].

LOOP AT la_item INTO lb_item.

MOVE lb_item-item_text TO lv_item_text.

LOOP AT lt_item INTO ls_item

WHERE item_text = lv_item_text.

--- --

- ---

ENDLOOP.

ENDLOOP.

You would just have to make sure that you didn't go into the processing code when you reach the same line in table lt_item as you are currently at in table la_item (check for a matching key if the table has one).

- April

Read only

0 Likes
637

yes, u r right. But already 4 nested loops are going on.. is there any problem if the number increases by 1 or 2. Performance point of view.