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

Loop fields/components of structure

Former Member
30,951

Hi there,

I want to code something for "looping" through a structure, I mean that I get every field of a structure and its value.

Now I have it like this:



  DO.

    ADD 1 TO lv_comp_count.
    ASSIGN COMPONENT lv_comp_count OF STRUCTURE is_data TO <fs_comp>.
    IF sy-subrc NE 0.
      EXIT.
    ENDIF.

* ....

  ENDDO.

But here I have the problem that I don't know the name of the field in this coding. How can I code this similar so that I get both: value and name of the field??

Thanks!

Regards,

Markus

3 REPLIES 3
Read only

Former Member
0 Likes
10,363

Hi,

perhaps the following funtion module will help you :

CALL FUNCTION 'GET_COMPONENT_LIST'

EXPORTING

program = sy-repid

fieldname = <name_of_you_struture>

TABLES

components = lt_components.

Regards.

David

Read only

Former Member
0 Likes
10,363

And one hard-code for example

  DATA: COMP LIKE  RSTRUCINFO OCCURS 0 WITH HEADER LINE.
  CALL 'AB_STRUC_INFO'
    ID 'PROGRAM'   FIELD SY-REPID
    ID 'STRUCNAME' FIELD 'structure name'
    ID 'STRUCINFO' FIELD COMP-*SYS*.
  LOOP AT COMP.
    ...
  ENDLOOP.

.

Return table with fields of internal structure in program.

Good luck.

Read only

former_member715866
Discoverer
10,363

Hana dynamic implementation:

FIELD-SYMBOLS: <lv_field> type any.
data: lr_descr_struc TYPE REF TO data.
data: lo_structdescr TYPE REF TO cl_abap_structdescr.

CLEAR: lr_descr_struc, lo_structdescr.
CREATE DATA lr_descr_struc like ch_service_datax. " <- any type
lo_structdescr ?= cl_abap_structdescr=>describe_by_data_ref( p_data_ref = lr_descr_struc ).
LOOP AT lo_structdescr->components ASSIGNING FIELD-SYMBOL(<lv_component>).
if <lv_component>-name(1) eq 'Z'.
ASSIGN COMPONENT <lv_component>-name of STRUCTURE ch_service_datax to <lv_field>.
if <lv_field> is ASSIGNED.
<lv_field> = abap_true.
endif.
endif.
ENDLOOP.