‎2007 Dec 13 11:02 AM
Hi ,
can any one help me , in nested loops .. how to avoid this one.. its urgent...
the code is as follows..........
OOP AT gt_indata INTO gs_indata.
Get Period Values from Header
IF ld_first_time = gc_J.
ld_first_time = gc_N.
Split input Header into Period Structure
FIELD-SYMBOLS: <lfs_hex> TYPE x,
<lfs_char> TYPE c.
ASSIGN gc_tab TO <lfs_hex>.
ASSIGN <lfs_hex> TO <lfs_char> CASTING.
Perform split1.
CLEAR: gs_split_1.
SPLIT gs_indata AT <lfs_char> INTO
gs_split_1-head_1
gs_split_1-head_2
gs_split_1-head_3
gs_split_1-head_4
*Period 1 to 10
gs_split_1-per_1 gs_split_1-per_2 gs_split_1-per_3 gs_split_1-per_4
gs_split_1-per_5 gs_split_1-per_6 gs_split_1-per_7 gs_split_1-per_8
gs_split_1-per_9 gs_split_1-per_10
*Period 11 to 20
gs_split_1-per_11 gs_split_1-per_12 gs_split_1-per_13 gs_split_1-per_14
gs_split_1-per_15 gs_split_1-per_16 gs_split_1-per_17 gs_split_1-per_18
gs_split_1-per_19 gs_split_1-per_20
*Period 21 to 30
gs_split_1-per_21 gs_split_1-per_22 gs_split_1-per_23 gs_split_1-per_24
gs_split_1-per_25 gs_split_1-per_26 gs_split_1-per_27 gs_split_1-per_28
gs_split_1-per_29 gs_split_1-per_30
*Period 31 to 40
gs_split_1-per_31 gs_split_1-per_32 gs_split_1-per_33 gs_split_1-per_34
gs_split_1-per_35 gs_split_1-per_36 gs_split_1-per_37 gs_split_1-per_38
gs_split_1-per_39 gs_split_1-per_40
*Period 41 to 50
gs_split_1-per_41 gs_split_1-per_42 gs_split_1-per_43 gs_split_1-per_44
gs_split_1-per_45 gs_split_1-per_46 gs_split_1-per_47 gs_split_1-per_48
gs_split_1-per_49 gs_split_1-per_50
*Period 51 to 55
gs_split_1-per_51 gs_split_1-per_52 gs_split_1-per_53 gs_split_1-per_54
gs_split_1-per_55.
*Create internal table with all Periods from input file Header
CLEAR ld_tabix.
Do 56 times Varying
WHILE w_tabix LT 56
gs_period-per FROM gs_split_1-per_1 NEXT gs_split_1-per_2.
ADD 1 TO ld_tabix.
*Break at value END in the file
IF gs_period-per = gc_END.
EXIT.
ENDIF.
APPEND gs_period TO gt_period.
ENDWHILE.
Enddo.
*Convert Date type and and Period to real Date
LOOP AT gt_period INTO gs_period.
CASE gs_period-per(1).
WHEN gc_W.
Week
MOVE gs_period-per+1(6) TO ld_week.
CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
week = ld_week
IMPORTING
date = ld_date
EXCEPTIONS
week_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
Error Wrong Period Week
Message A000(38) with text-002.
ENDIF.
MOVE gc_2 TO gs_period-date_type.
MOVE ld_date TO gs_period-req_date.
MODIFY gt_period FROM gs_period.
WHEN gc_M.
Month
MOVE gc_3 TO gs_period-date_type.
MOVE gs_period-per1(6) TO ld_date_alfn0(6).
MOVE 01 TO ld_date_alfn+6(2).
MOVE ld_date_alfn TO ld_date.
Check if Date is Valid
CALL FUNCTION 'LAST_DAY_OF_MONTHS'
EXPORTING
day_in = ld_date
IMPORTING
last_day_of_month = ld_date_last
EXCEPTIONS
day_in_no_date = 1
OTHERS = 2.
IF sy-subrc <> 0.
Error Wrong Period Month
Message A000(38) with text-003.
ENDIF.
MOVE ld_date TO gs_period-req_date.
MODIFY gt_period FROM gs_period.
WHEN OTHERS.
Error Wrong Date Type
MESSAGE A000(38) WITH text-001.
ENDCASE.
ENDLOOP.
DESCRIBE TABLE gt_period LINES gs_max_line.
CONTINUE.
ENDIF.
*Split input Item into Field Structure
ASSIGN gc_tab TO <lfs_hex>.
ASSIGN <lfs_hex> TO <lfs_char> CASTING.
CLEAR: gs_split_2,
gt_quantity.
SPLIT gs_indata AT <lfs_char> INTO
gs_split_2-material
gs_split_2-plant
gs_split_2-version
gs_split_2-act_ind
*Period 1 to 10
gs_split_2-ant_1 gs_split_2-ant_2 gs_split_2-ant_3 gs_split_2-ant_4
gs_split_2-ant_5 gs_split_2-ant_6 gs_split_2-ant_7 gs_split_2-ant_8
gs_split_2-ant_9 gs_split_2-ant_10
*Period 11 to 20
gs_split_2-ant_11 gs_split_2-ant_12 gs_split_2-ant_13 gs_split_2-ant_14
gs_split_2-ant_15 gs_split_2-ant_16 gs_split_2-ant_17 gs_split_2-ant_18
gs_split_2-ant_19 gs_split_2-ant_20
*Period 21 to 30
gs_split_2-ant_21 gs_split_2-ant_22 gs_split_2-ant_23 gs_split_2-ant_24
gs_split_2-ant_25 gs_split_2-ant_26 gs_split_2-ant_27 gs_split_2-ant_28
gs_split_2-ant_29 gs_split_2-ant_30
*Period 31 to 40
gs_split_2-ant_31 gs_split_2-ant_32 gs_split_2-ant_33 gs_split_2-ant_34
gs_split_2-ant_35 gs_split_2-ant_36 gs_split_2-ant_37 gs_split_2-ant_38
gs_split_2-ant_39 gs_split_2-ant_40
*Period 41 to 50
gs_split_2-ant_41 gs_split_2-ant_42 gs_split_2-ant_43 gs_split_2-ant_44
gs_split_2-ant_45 gs_split_2-ant_46 gs_split_2-ant_47 gs_split_2-ant_48
gs_split_2-ant_49 gs_split_2-ant_50
*Period 51 to 55
gs_split_2-ant_51 gs_split_2-ant_52 gs_split_2-ant_53 gs_split_2-ant_54
gs_split_2-ant_55.
*Create internal table with all Periods from input file
CLEAR ld_tabix.
WHILE w_tabix LT gs_max_line
Do gs_max_line Times Varying
gs_quantity-antal FROM gs_split_2-ant_1 NEXT gs_split_2-ant_2.
ADD 1 TO ld_tabix.
APPEND gs_quantity TO gt_quantity.
Enddo.
ENDWHILE.
MOVE gs_split_2-material TO gs_outdata-matnr.
MOVE gs_split_2-plant TO gs_outdata-plant.
MOVE gs_split_2-version TO gs_outdata-version.
MOVE gs_split_2-act_ind TO gs_outdata-act_ind.
MOVE gt_quantity[] TO gs_outdata-quantity[].
APPEND gs_outdata TO gt_outdata.
ENDLOOP. "GT_INDATA
Loop through Output file records and create return table for each Product
Allocation Object.
LOOP AT gt_alloc_object INTO gs_alloc_object.
LOOP AT gt_outdata INTO gs_outdata.
Read Table gt_outdata into gs_outdata index sy-index.
CLEAR gs_t_data.
gs_t_data-konob = gs_alloc_object-konob.
gs_t_data-matnr = gs_outdata-matnr.
gs_t_data-vrsio = gs_outdata-version.
gs_t_data-werks = gs_outdata-plant.
Validate material and get unit of measurement from material master
SELECT SINGLE meins FROM mara
INTO gs_t_data-basme
WHERE matnr = gs_t_data-matnr.
IF sy-subrc EQ 0.
Material exists
LOOP AT gs_outdata-quantity INTO gs_quantity.
READ TABLE gt_outdata into gs_outdata index sy-index.
move gs_outdata-quantity to gs_quantity-antal.
MOVE gs_quantity-antal TO gs_t_data-kcqty.
Get period
SORT gt_period.
READ TABLE gt_period INTO gs_period INDEX sy-tabix .
IF SY-SUBRC = 0.
gs_t_data-spwoc = gs_period-per+1(6).
ENDIF.
Check restrictions
IF gs_t_data-werks IN gt_werks AND
gs_t_data-matnr IN gt_matnr AND
gs_t_data-spwoc IN gt_spwoc.
APPEND gs_t_data TO t_data.
ENDIF.
ENDLOOP. "gs_OUTDATA-QUANTITY
ENDIF.
ENDLOOP. "GT_OUTDATA
ENDLOOP. "GT_ALLOC_OBJECT
ENDIF.
ENDIF.
WHEN OTHERS.
No more info structures implemented
ENDCASE.
form split1.
endform.
‎2007 Dec 13 12:44 PM
You can not avoid nested loops, because it is often necessary to compare to internal tables,
1. One table must be the outher table and must be fully processed,
Each each line of the outer table:
You must not process a full inner table.
But only one line of the inner table, then use READ ... BINARY SEARCH' and SORT this table once outside of the outer table!
Or a few lines of the inner table, then use READ BINARY SEARCH and LOOP ... FROM INDEX with EXIT condition, which is very important.
Read this blog, especially the last section:
/people/siegfried.boes/blog/2007/09/12/runtimes-of-reads-and-loops-on-internal-tables
Siegfried
‎2007 Dec 13 11:53 AM
hi
You can use read statement using binary search instead of using nested loop in some places at ur program. but dont forget to sort the table which u r reading before reading that.
One more thing in side the loop u used select single *. But u can use select with required fields out side of the loop in to itab, then read that itab in side the loop.
Reward if it dose.
Thanks
Siva Kumar
‎2007 Dec 13 12:44 PM
You can not avoid nested loops, because it is often necessary to compare to internal tables,
1. One table must be the outher table and must be fully processed,
Each each line of the outer table:
You must not process a full inner table.
But only one line of the inner table, then use READ ... BINARY SEARCH' and SORT this table once outside of the outer table!
Or a few lines of the inner table, then use READ BINARY SEARCH and LOOP ... FROM INDEX with EXIT condition, which is very important.
Read this blog, especially the last section:
/people/siegfried.boes/blog/2007/09/12/runtimes-of-reads-and-loops-on-internal-tables
Siegfried
‎2007 Dec 20 4:48 AM
For better performance go for Parellel cursor technique or fetch data in internal tables using Select .... FOR all enteries....
Regards
Naveen Gupta
‎2007 Dec 20 4:56 AM
‎2007 Dec 20 8:59 AM
Hi,
using parllel cursor technique you can avoid nested loop.
for example:-
data: l_index like sy-index.
loop at itab.
read table itab2 with key ...........
if sy-subrc = 0.
l_index = sy-index.
do.
"Here write the code what you want".
again go for read.
l_index = l_index + 1
read table itab2 index l_index.
if compare the all fields which you are using while
reading first time from the table itab2.
else.
exit.
endif.
enddo.
clear l_index.
endif.
endloop.
Reward if useful.
Thanks,
Sreeram.
‎2007 Dec 20 9:04 AM
Hi,
The exact Parllel cursor technique is :
SORT : it_mara BY matnr ,
it_makt BY matnr .
i = 1.
LOOP AT it_mara INTO wa_mara.
LOOP AT it_makt INTO wa_makt FROM i.
IF wa_makt-matnr <> wa_mara-matnr.
i = sy-tabix.
EXIT.
ENDIF.
write 😕 wa_mara-matnr , wa_makt-MAKTX .
MOVE wa_mara-matnr TO itab-matnr.
MOVE wa_makt-maktx TO itab-maktx.
append itab .
ENDLOOP.
ENDLOOP.
PS: REward points if helpfull
Regards
Naveen Gupta