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

Rewrite the LOOP through FOR or REDUCE

friendlycoder
Participant
0 Likes
883

Hi all

Is it possible to rewrite this code snippet with FOR or REDUCE?

LOOP AT t_customres REFERENCE INTO DATA(ls_customer).
 IF line_exists( t_routes[ route = ls_customer->route ] ) .
 CONTINUE.
 ENDIF.
 INSERT ls_customer->* INTO TABLE lt_filtered_customers.
ENDLOOP.

The whole program:

TYPES:
 BEGIN OF ty_customer,
 customer TYPE char10,
 name TYPE char30,
 city TYPE char30,
 route TYPE char10,
 END OF ty_customer.
TYPES: tt_customers TYPE SORTED TABLE OF ty_customer
 WITH UNIQUE KEY customer.
TYPES:
 BEGIN OF ty_routes,
 route TYPE char10,
 name TYPE char40,
 END OF ty_routes.
TYPES: tt_routes TYPE SORTED TABLE OF ty_routes
 WITH UNIQUE KEY route.
DATA(t_customres) =
 VALUE tt_customers(
 ( customer = 'C0001' name = 'Test Customer 1' city = 'NY' route = 'R0001' )
 ( customer = 'C0002' name = 'Customer 2' city = 'LA' route = 'R0003' )
 ( customer = 'C0003' name = 'Good Customer 3' city = 'DFW' route = 'R0001' )
 ( customer = 'C0004' name = 'Best Customer 4' city = 'CH' route = 'R0003' )
 ( customer = 'C0005' name = 'Best Customer 4' city = 'CH' route = 'R0005' )
 ( customer = 'C0006' name = 'Best Customer 4' city = 'CH' route = 'R0005' )
 ).
DATA(t_routes) =
 VALUE tt_routes(
 ( route = 'R0001' name = 'Route 1' )
 ( route = 'R0002' name = 'Route 2' )
 ( route = 'R0003' name = 'Route 3' )
 ).
DATA(lt_filtered_customers) = VALUE tt_customers( ).
LOOP AT t_customres REFERENCE INTO DATA(ls_customer).
 IF line_exists( t_routes[ route = ls_customer->route ] ) .
 CONTINUE.
 ENDIF.
 INSERT ls_customer->* INTO TABLE lt_filtered_customers.
ENDLOOP.

Thanks

Hi all

Is it possible to rewrite this code snippet with FOR or REDUCE?

LOOP AT t_customres REFERENCE INTO DATA(ls_customer).
 IF line_exists( t_routes[ route = ls_customer->route ] ) .
 CONTINUE.
 ENDIF.
 INSERT ls_customer->* INTO TABLE lt_filtered_customers.
ENDLOOP.

The whole program:

TYPES:
 BEGIN OF ty_customer,
 customer TYPE char10,
 name TYPE char30,
 city TYPE char30,
 route TYPE char10,
 END OF ty_customer.
TYPES: tt_customers TYPE SORTED TABLE OF ty_customer
 WITH UNIQUE KEY customer.
TYPES:
 BEGIN OF ty_routes,
 route TYPE char10,
 name TYPE char40,
 END OF ty_routes.
TYPES: tt_routes TYPE SORTED TABLE OF ty_routes
 WITH UNIQUE KEY route.
DATA(t_customres) =
 VALUE tt_customers(
 ( customer = 'C0001' name = 'Test Customer 1' city = 'NY' route = 'R0001' )
 ( customer = 'C0002' name = 'Customer 2' city = 'LA' route = 'R0003' )
 ( customer = 'C0003' name = 'Good Customer 3' city = 'DFW' route = 'R0001' )
 ( customer = 'C0004' name = 'Best Customer 4' city = 'CH' route = 'R0003' )
 ( customer = 'C0005' name = 'Best Customer 4' city = 'CH' route = 'R0005' )
 ( customer = 'C0006' name = 'Best Customer 4' city = 'CH' route = 'R0005' )
 ).
DATA(t_routes) =
 VALUE tt_routes(
 ( route = 'R0001' name = 'Route 1' )
 ( route = 'R0002' name = 'Route 2' )
 ( route = 'R0003' name = 'Route 3' )
 ).
DATA(lt_filtered_customers) = VALUE tt_customers( ).
LOOP AT t_customres REFERENCE INTO DATA(ls_customer).
 IF line_exists( t_routes[ route = ls_customer->route ] ) .
 CONTINUE.
 ENDIF.
 INSERT ls_customer->* INTO TABLE lt_filtered_customers.
ENDLOOP.

Thanks

1 REPLY 1
Read only

DoanManhQuynh
Active Contributor
724

idealy you could do like this:

DATA(lt_filtered_customers1) = VALUE tt_customers( FOR ls_customer1 IN t_customres
                                                   ( COND #( WHEN NOT line_exists( t_routes[ route = ls_customer1-route ] ) THEN ls_customer1 ) ) ).