06-21-2007 2:13 PM
Hi,
I wanted to create an internal table that contains an id and a field symbol.
something like:
types: begin of my_type,
id(2) type c,
field-symbols: <fs> type something.
end of my_type.
data: my_table type standard table of my_type,
wa type my_type.
wa-id = 'B'.
wa-fs = <fs_second>.
I know won't pass the checker, but I'd like to know if it was possible. I'm able to use references, but I want field symbols so I will avoid reading a huge table or create secondary indexes....
Thanks for the help,
Isidoro!
06-21-2007 2:27 PM
Here is the with Example and explanation also ...
DATA: BEGIN OF LINE,
COL1 TYPE I VALUE '11',
COL2 TYPE I VALUE '22',
COL3 TYPE I VALUE '33',
END OF LINE.
DATA COMP(5) VALUE 'COL3'.
FIELD-SYMBOLS: <F1>, <F2>, <F3>.
ASSIGN LINE TO <F1>.
ASSIGN COMP TO <F2>.
DO 3 TIMES.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE <F1> TO <F3>.
WRITE <F3>.
ENDDO.
ASSIGN COMPONENT <F2> OF STRUCTURE <F1> TO <F3>.
WRITE / <F3>.
The output is:
11 22 33
33
The field symbol <F1> points to the structure LINE, <F2> points to the field COMP. In the DO loop, the components of LINE are specified by their numbers and assigned one by one to <F3>. After the loop, the component COL3 of LINE is specified by its name and assigned to <F3>. Note that ASSIGN COMPONENT is the only possible method of addressing the components of <F1>. Expressions such as <F1>-COL1 are syntactically incorrect. The main thing is ASSIGN statement sets the sy-subrc.
i.e u can also write the DO loop as...
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE <F1> TO <F3>.
if sy-subrc ne 0.
exit.
endif.
WRITE <F3>.
ENDDO.
*******************************************
this is a simple use of the field symbols
*******************************************
FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE i.
DATA: text(20) TYPE c VALUE 'Hello, how are you?',
num TYPE i VALUE 5,
BEGIN OF line1,
col1 TYPE f VALUE '1.1e+10',
col2 TYPE i VALUE '1234',
END OF line1,
line2 LIKE line1.
ASSIGN text TO <f1>.
ASSIGN num TO <f2>.
DESCRIBE FIELD <f1> LENGTH <f2>.
WRITE: / <f1>, 'has length', num.
ASSIGN line1 TO <f1>.
ASSIGN line2-col2 TO <f2>.
MOVE <f1> TO line2.
ASSIGN 'LINE2-COL2 =' TO <f1>.
WRITE: / <f1>, <f2
>.
The output is:
Hello, how are you? has length 20
LINE-COL2 = 1,234
The example declares two field symbols <F1> and <F2>. <F2> has the type I, which means that only type I fields may be assigned to it. <F1> and <F2> both point to different fields during the program.
reward points if it is usefull ....
Girish
06-21-2007 2:21 PM
AFAIK you can't declare a Field-Symbols inside an Internal Table....
What you can do is declare a Field-Symbol to use only the header...like this....
TYPE: BEGIN OF TY_DATA,
FIELD1 TYPE C,
FIELD2 TYPE C,
END OF TY_DATA.
DATA: DATA TYPE STANDARD TABLE OF TY_DATA WITH HEADER LINE.
FIELD-SYMBOLS: <DATA> LIKE LINE OF T_DATA.
LOOP AT T_DATA ASSIGNING <DATA>.
*Do something...
ENDLOOP.
Greetings,
Blag.
06-21-2007 2:27 PM
Here is the with Example and explanation also ...
DATA: BEGIN OF LINE,
COL1 TYPE I VALUE '11',
COL2 TYPE I VALUE '22',
COL3 TYPE I VALUE '33',
END OF LINE.
DATA COMP(5) VALUE 'COL3'.
FIELD-SYMBOLS: <F1>, <F2>, <F3>.
ASSIGN LINE TO <F1>.
ASSIGN COMP TO <F2>.
DO 3 TIMES.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE <F1> TO <F3>.
WRITE <F3>.
ENDDO.
ASSIGN COMPONENT <F2> OF STRUCTURE <F1> TO <F3>.
WRITE / <F3>.
The output is:
11 22 33
33
The field symbol <F1> points to the structure LINE, <F2> points to the field COMP. In the DO loop, the components of LINE are specified by their numbers and assigned one by one to <F3>. After the loop, the component COL3 of LINE is specified by its name and assigned to <F3>. Note that ASSIGN COMPONENT is the only possible method of addressing the components of <F1>. Expressions such as <F1>-COL1 are syntactically incorrect. The main thing is ASSIGN statement sets the sy-subrc.
i.e u can also write the DO loop as...
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE <F1> TO <F3>.
if sy-subrc ne 0.
exit.
endif.
WRITE <F3>.
ENDDO.
*******************************************
this is a simple use of the field symbols
*******************************************
FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE i.
DATA: text(20) TYPE c VALUE 'Hello, how are you?',
num TYPE i VALUE 5,
BEGIN OF line1,
col1 TYPE f VALUE '1.1e+10',
col2 TYPE i VALUE '1234',
END OF line1,
line2 LIKE line1.
ASSIGN text TO <f1>.
ASSIGN num TO <f2>.
DESCRIBE FIELD <f1> LENGTH <f2>.
WRITE: / <f1>, 'has length', num.
ASSIGN line1 TO <f1>.
ASSIGN line2-col2 TO <f2>.
MOVE <f1> TO line2.
ASSIGN 'LINE2-COL2 =' TO <f1>.
WRITE: / <f1>, <f2
>.
The output is:
Hello, how are you? has length 20
LINE-COL2 = 1,234
The example declares two field symbols <F1> and <F2>. <F2> has the type I, which means that only type I fields may be assigned to it. <F1> and <F2> both point to different fields during the program.
reward points if it is usefull ....
Girish
06-21-2007 2:35 PM
Hi,
I made this class yesterday to a friend about generic type, maybe this can help you too.
*------ Class some_class definition
CLASS some_class DEFINITION.
PUBLIC SECTION.
METHODS write_dados.
METHODS manual_ins IMPORTING id TYPE scustom-id
name TYPE scustom-name date TYPE sy-datum.
METHODS add_values IMPORTING tabela TYPE any.
PRIVATE SECTION.
TYPES: BEGIN OF dados,
id TYPE scustom-id,
name TYPE scustom-name,
date TYPE sy-datum,
END OF dados.
DATA: t_dados TYPE TABLE OF dados.
ENDCLASS. "some_class DEFINITION
*------ class implementation
CLASS some_class IMPLEMENTATION.
*------ method add values
METHOD add_values.
FIELD-SYMBOLS: <wa> TYPE ANY,
<fstable> TYPE table.
ASSIGN tabela TO <fstable>.
LOOP AT <fstable> ASSIGNING <wa>.
APPEND <wa> TO t_dados.
ENDLOOP.
ENDMETHOD. "some_class
*------ method manual insert
METHOD manual_ins.
DATA: temp TYPE string.
FIELD-SYMBOLS: <wa> TYPE ANY,
<fstable> TYPE table.
ASSIGN t_dados TO <fstable>.
READ TABLE t_dados INDEX 1 ASSIGNING <wa>.
temp = <wa>.
<wa>+0(8) = id.
<wa>+8(25) = name.
<wa>+33(8) = date.
APPEND <wa> TO <fstable>.
<wa> = temp.
ENDMETHOD. "some_class
*------ method write dados
METHOD write_dados.
FIELD-SYMBOLS <wa> TYPE ANY.
LOOP AT t_dados ASSIGNING <wa>.
WRITE: / <wa>0(8), <wa>8(25), <wa>+33(8).
ENDLOOP.
ENDMETHOD. "some_class
ENDCLASS. "some_class IMPLEMENTATION
*------ program start
START-OF-SELECTION.
*------ vars
DATA: o TYPE REF TO some_class.
DATA: BEGIN OF ti_scustom OCCURS 0,
id LIKE scustom-id,
name LIKE scustom-name,
date LIKE sy-datum,
END OF ti_scustom.
CREATE OBJECT o.
SELECT id name FROM scustom INTO CORRESPONDING FIELDS OF ti_scustom.
ti_scustom-date = sy-datum.
APPEND ti_scustom.
ENDSELECT.
*------ add values of itab into class' itab
IF sy-subrc EQ 0.
CALL METHOD o->add_values( ti_scustom[] ).
ENDIF.
*------ insert the record manualy
CALL METHOD o->manual_ins
EXPORTING
id = 332
name = 'TESTE'
date = sy-datum.
*------ display the content of itab
CALL METHOD o->write_dados.
END-OF-SELECTION.
06-21-2007 2:39 PM
Hello,
You cannot have field symbols in internal table. But an alternate will be to have a data of type class 'DATA'. Verify the code below and let me know if it helps:
REPORT z_out.
TABLES: t001.
DATA: BEGIN OF itab OCCURS 0.
DATA: id TYPE i.
DATA: obj TYPE REF TO data.
DATA: END OF itab.
FIELD-SYMBOLS: <fs> TYPE ANY.
FIELD-SYMBOLS: <fs1> TYPE ANY.
itab-id = 1.
APPEND itab.
itab-id = 2.
APPEND itab.
LOOP AT itab.
WRITE:/ itab-id.
ULINE.
CREATE DATA itab-obj LIKE t001.
ASSIGN itab-obj->* TO <fs>.
SELECT SINGLE * INTO <fs> FROM t001.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs> TO <fs1>.
IF sy-subrc = 0.
WRITE: <fs1>.
ELSE.
EXIT.
ENDIF.
ENDDO.
ULINE.
ENDLOOP.
Regards,
S. Chandra Mouli.