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

Checking different fields of a work area inside a loop

Former Member
0 Likes
3,036

Hi,

In the program, I have populated an internal table itab1, with 11 fields  as," key1, F1, F2,....F10 ".

Now, I am looping inside another table, say itab2. I want to check each of the above 10 fields, inside the loop, against another table itab3,

as below.

Loop at itab2.

read table itab1 with key key1 = itab2-key1.

if sy-subrc= 0.

read table itab3 with key F1 = itab1-F1.

if -sy-subrc = 0.

write : 'success F1'

endif.

read table itab3 with key F2 = itab1-F2.

if -sy-subrc = 0.

write : 'success F2'

endif.

:

:

read table itab3 with key F10 = itab1-F10.

if -sy-subrc = 0.

write : 'success F10'

endif.

endloop.

My question is, instead of writing 10 times(where we check F1, F2,----F10), can we enhance the above logic? where we can achive this with lesser statements and avoid repetition?

Regards

SR


1 ACCEPTED SOLUTION
Read only

Former Member
1,282

Hi Swastik,

You could write a subroutine importing field name& value and changing subrc.

data: go_descr TYPE REF TO cl_abap_tabledescr.

field-symbols: <fs> type  abap_keydescr,

                     <fs2> type any.

LOOP AT itab2.

read table itab1 into wa_itab1 with key key1 = itab2-key1.

if sy-subrc= 0.

  go_descr ?= cl_abap_typedescr=>describe_by_data( itab1 ).

  LOOP AT go_descr->key ASSIGNING <fs>.

     ASSIGN COMPONENTS <fs>-name of structure wa_itab1 into <fs2>

       PERFORM check_fields using <fs>-name itab1changing gv_subrc.

  ENDLOOP.

endif.

ENDLOOP.

FORM f_check_fields using field_name type char10 field_value type char10 changing subrc type i.

read table itab3 with key (field_name)= field_value.

subrc = sy-subrc.

ENDFORM.

Maybe have little bug.

regards,

Archer

Hi Swastik,

You could write a subroutine importing field name& value and changing subrc.

data: go_descr TYPE REF TO cl_abap_tabledescr.

field-symbols: <fs> type  abap_keydescr,

                     <fs2> type any.

LOOP AT itab2.

read table itab1 into wa_itab1 with key key1 = itab2-key1.

if sy-subrc= 0.

  go_descr ?= cl_abap_typedescr=>describe_by_data( itab1 ).

  LOOP AT go_descr->key ASSIGNING <fs>.

     ASSIGN COMPONENTS <fs>-name of structure wa_itab1 into <fs2>

       PERFORM check_fields using <fs>-name itab1changing gv_subrc.

  ENDLOOP.

endif.

ENDLOOP.

FORM f_check_fields using field_name type char10 field_value type char10 changing subrc type i.

read table itab3 with key (field_name)= field_value.

subrc = sy-subrc.

ENDFORM.

Maybe have little bug.

regards,

Archer

2 REPLIES 2
Read only

Former Member
1,283

Hi Swastik,

You could write a subroutine importing field name& value and changing subrc.

data: go_descr TYPE REF TO cl_abap_tabledescr.

field-symbols: <fs> type  abap_keydescr,

                     <fs2> type any.

LOOP AT itab2.

read table itab1 into wa_itab1 with key key1 = itab2-key1.

if sy-subrc= 0.

  go_descr ?= cl_abap_typedescr=>describe_by_data( itab1 ).

  LOOP AT go_descr->key ASSIGNING <fs>.

     ASSIGN COMPONENTS <fs>-name of structure wa_itab1 into <fs2>

       PERFORM check_fields using <fs>-name itab1changing gv_subrc.

  ENDLOOP.

endif.

ENDLOOP.

FORM f_check_fields using field_name type char10 field_value type char10 changing subrc type i.

read table itab3 with key (field_name)= field_value.

subrc = sy-subrc.

ENDFORM.

Maybe have little bug.

regards,

Archer

Read only

oliver_wurm
Active Participant
0 Likes
1,282

Hi Swastik,

you can try with:

DATA: lv_comp(2) TYPE n,

       lv_fieldname(30) TYPE c.

FIELD-SYMBOLS: <fs>.

LOOP AT itab2.

   READ TABLE itab1 WITH KEY key1 = itab2-key1.

   IF sy-subrc = 0.

     lv_comp = 1.

     DO 10 TIMES.

       CONCATENATE 'F' lv_comp INTO lv_fieldname.

       ADD 1 TO lv_comp.

       ASSIGN COMPONENT lv_comp OF STRUCTURE itab1 TO <fs>.

       READ TABLE itab3 WITH KEY (lv_fieldname) = <fs>.

       IF sy-subrc = 0.

         WRITE:/ 'Success', lv_fieldname.

       ENDIF.

     ENDDO.

   ENDIF.

ENDLOOP.


Regards

Oliver