Application Development 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: 

how to get field name for a specific value in internal table

Former Member
0 Kudos

Hello All,

I want to know how should I get the name of the field for a matching entry in the internal table I mean this : I have table called IT1 and IT1 has 4 fields.

Data: begin of it1 occur 0,

printer(10) type c,

paper1(20) type c,

paper2(20) type c,

paper3(20) type c,

end of it1.

Now it1 has 2 records like :

Printer Paper1 Paper2 Paper3

MUC123 KBLOGO BLANK DINA5

MUC124 DINA5 BLANK KBLOGO

now in internal table I want to know, for value KBLOGO for the first record what is the name of the field(In this case it is "Paper1") and for the second record also the name of the field(In this case it is "Paper3").

I can tell you because I can see KBLOGO exists under which field name but dynamically this value or other value may fall under any field name.

So how to fetch the field name for a record for a matching value.

I hope I am clear with my question.

Thanks,

Mark

5 REPLIES 5

Former Member
0 Kudos

Hello Mark,

This is what u want.


REPORT ZV_FIND_FIELD .

DATA: BEGIN OF IT1 OCCURS 0,
PRINTER(10) TYPE C,
PAPER1(20) TYPE C,
PAPER2(20) TYPE C,
PAPER3(20) TYPE C,
END OF IT1.

IT1-PRINTER = 'MUC123'.
IT1-PAPER1 = 'KBLOGO'.
IT1-PAPER2 = 'BLANK'.
IT1-PAPER3 = 'DINA5'.

APPEND IT1.

IT1-PRINTER = 'MUC123'.
IT1-PAPER3 = 'KBLOGO'.
IT1-PAPER2 = 'BLANK'.
IT1-PAPER1 = 'DINA5'.

APPEND IT1.

LOOP AT IT1.
  CASE 'KBLOGO'.
    WHEN IT1-PAPER1.
      WRITE:/'Paper1 contains KBLOGO'.
    WHEN IT1-PAPER2.
      WRITE:/ 'Paper2 contains KBLOGO'.
    WHEN IT1-PAPER3.
      WRITE:/ 'Paper3 contains KBLOGO'.
  ENDCASE.
ENDLOOP.

Vasanth

Pawan_Kesari
Active Contributor
0 Kudos

refer below program


DATA: BEGIN OF STR, 
          A VALUE 'a', 
          B VALUE 'b', 
          C VALUE 'c', 
          D VALUE 'd', 
        END   OF STR, 
        CN(5) VALUE 'D'. 
  FIELD-SYMBOLS <FS> TYPE ANY. 
  DO 3 TIMES. 
    ASSIGN COMPONENT SY-INDEX OF 
           STRUCTURE STR TO <FS>. 
    IF SY-SUBRC <> 0. EXIT. ENDIF. 
    WRITE <FS>. 
  ENDDO. 
  ASSIGN COMPONENT CN OF STRUCTURE STR TO <FS>. 
  WRITE <FS>. 

I think you can use statement <b>ASSIGN COMPONENT CN OF STRUCTURE STR TO <FS>.</b> or <b>ASSIGN COMPONENT SY-INDEX OF

STRUCTURE STR TO <FS>.</b> in your case.

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Hope this works for ya.



report zrich_0003 .


data: begin of it1 occurs 0,
       printer(10) type c,
       paper1(20) type c,
       paper2(20) type c,
       paper3(20) type c,
      end of it1.

data: fieldlist type table of rstrucinfo with header line.

data: repid type sy-repid.

field-symbols: <fs>.


it1-printer = 'MUC123'.
it1-paper1  = 'KBLOGO'.
it1-paper2  = 'BLANK'.
it1-paper3  = 'DINA5'.
append it1.

it1-printer = 'MUC124'.
it1-paper1  = 'DINA5'.
it1-paper2  = 'BLANK'.
it1-paper3  = 'KBLOGO'.
append it1.


repid = sy-repid.

call function 'GET_COMPONENT_LIST'
     exporting
          program    = repid
          fieldname  = 'it1'
     tables
          components = fieldlist.


loop at it1.

  do.
    assign component sy-index of structure it1 to <fs>.
    if sy-subrc <> 0.
      exit.
    endif.
    if <fs> = 'KBLOGO'.
      read table fieldlist index sy-index.
      if sy-subrc = 0.
        write:/ fieldlist-compname.
      endif.
    endif.
  enddo.

endloop.

Regards

Rich Heilman

ferry_lianto
Active Contributor
0 Kudos

Hi,

Please try this ...


DATA: WA_POS LIKE SY-FDPOS.

LOOP AT IT1.
  SEARCH IT1 FOR 'KBLOGO'.

  CLEAR WA_POS.
  WA_POS = SY-FDPOS + 1.

  CASE WA_POS.
     WHEN '10'.
        WRITE: / 'KBLOGO IS IN FIELD PRINTER FOR RECORD: ', SY-TABIX.
 
    WHEN '30'.
        WRITE: / 'KBLOGO IS IN FIELD PAPER1 FOR RECORD: ', SY-TABIX.

     WHEN '50'.
        WRITE: / 'KBLOGO IS IN FIELD PAPER2 FOR RECORD: ', SY-TABIX.

     WHEN '70'.
        WRITE: / 'KBLOGO IS IN FIELD PAPER3 FOR RECORD: ', SY-TABIX.

  ENDCASE.
ENDLOOP.

Regards,

Ferry Lianto

Former Member
0 Kudos

Try this....

REPORT ZZDYNAMIC_FIELD.

DATA: BEGIN OF it1 OCCURS 0,

printer(10) TYPE c,

paper1(20) TYPE c,

paper2(20) TYPE c,

paper3(20) TYPE c,

END OF it1.

DATA: descr_ref TYPE REF TO cl_abap_tabledescr,

i TYPE i.

FIELD-SYMBOLS:

<key_comp_wa> TYPE abap_keydescr,

<field> TYPE ANY.

it1-printer = 'MUC123'.

it1-paper1 = 'KBLOGO'.

it1-paper2 = 'BLANK'.

it1-paper3 = 'DINA5'.

APPEND it1.

it1-printer = 'MUC123'.

it1-paper3 = 'KBLOGO'.

it1-paper2 = 'BLANK'.

it1-paper1 = 'DINA5'.

APPEND it1.

TRY.

descr_ref ?= cl_abap_typedescr=>describe_by_data( it1[] ).

CATCH cx_root.

ENDTRY.

LOOP AT it1.

i = 0.

WHILE 1 = 1.

i = i + 1.

ASSIGN COMPONENT i OF STRUCTURE it1 TO <field>.

if <field> is not assigned.

EXIT.

endif..

IF <field> IS ASSIGNED AND <field> EQ 'KBLOGO'.

READ TABLE descr_ref->key INDEX i ASSIGNING <key_comp_wa>.

WRITE <key_comp_wa>-name.

exit.

ENDIF.

ENDWHILE.