07-07-2008 4:29 PM
Hi experts,
i have a problem regarding to fieldsymbols:
I'm assigning a workarea to a fieldsymbol to get dynamically all fields and the values of the fields of a arbitrary itab into a stringtable line per line. The stringtable is used to create a csv.
e.g.
itab1
Field1|Field2|Field3
AAA |BBB |3
CCC |DDD |4
--> to itab2 (stringtable)
"AAA","BBB","3"
"CCC","DDD","4"
To get the value of the fieldsymbol i use:
Write <FS> to lv_char.
I'm doing this, because i can't just concatenate the <fs>, b/c if it is of type i at one moment, i'll get a dump.
My problem now is, that if <fs> is from type i at one moment the lv_char variable is empty after that. if the field is from type char, the value of the fieldsymbol is written to lv_char.
Coding:
i marked the part where i got the problem with:
"################################################
"#THIS IS THE PART WHERE I HAVE THE PROBLEM #
"################################################
REPORT Z_TEST0047418.
*sample type!!!
TYPES: BEGIN OF tt,
t1 TYPE c LENGTH 10,
t2 TYPE c LENGTH 20,
d TYPE i,
END OF tt.
**********************************************************************
* Define the structure of which a csv/tab file should be created *
**********************************************************************
DATA:
itab TYPE TABLE OF tt, "contains the data for the output file
struc TYPE tt. "has to be the structure of the itab
**********************************************************************
DATA:
it_pernrs TYPE TABLE OF pernr-pernr,
con_tab TYPE abap_char1
VALUE cl_abap_char_utilities=>horizontal_tab, "Tab-Delimiter
gv_sep TYPE c LENGTH 1, "Seperator.
lv_dirname TYPE string,
it_file TYPE TABLE OF string, "Outputfile - ITAB
wa_file TYPE string, "Outputfile - WA
it_fieldnames TYPE TABLE OF string, "Fieldnames of structure
wa_fieldnames TYPE string, "Fieldnames - WA
gv_lines TYPE syindex, "nr. of lines of itab
gv_ext TYPE string. "file extension
**********************************************************************
* sample input!!!
*--------------------------------------------------------------------*
struc-t1 = 'ABCDEFGHIJ'.
struc-t2 = '12345678901234567890'.
struc-d = 0.
APPEND struc TO itab.
struc-t1 = 'JKLMNOPQRS'.
struc-t2 = '12345678901234567890'.
struc-d = 0.
APPEND struc TO itab.
*--------------------------------------------------------------------*
"create file.
PERFORM create_output TABLES itab USING struc. "creates the csv/tab file out of the itab
*&---------------------------------------------------------------------*
*& Form CREATE_OUTPUT
*&---------------------------------------------------------------------*
*
* This form creates the tab delimited / comma seperated file
* It uses any itab and structure to create the file
*
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM create_output TABLES fv_itab USING fv_struc.
DATA: it_fields TYPE TABLE OF rfc_fields,
wa_fields LIKE LINE OF it_fields,
lv_lines TYPE syindex,
lv_cont TYPE c LENGTH 241,
lv_strlen TYPE i,
lv_off TYPE i.
FIELD-SYMBOLS: <fs_cont> TYPE ANY.
"get fields of structure
PERFORM get_structure TABLES it_fields USING fv_struc.
"create headline
CLEAR: it_file, wa_file.
LOOP AT it_fields INTO wa_fields.
CONCATENATE
wa_file
'"' wa_fields-fieldname '"'
',' INTO wa_file.
ENDLOOP.
"delete last seperator of headline
lv_strlen = STRLEN( wa_file ).
lv_off = lv_strlen - 1.
REPLACE SECTION OFFSET lv_off LENGTH 1 OF wa_file WITH ''.
"append headline to itab
APPEND wa_file TO it_file.
DESCRIBE TABLE it_fields LINES lv_lines.
"################################################
"#*THIS IS THE PART WHERE I HAVE THE PROBLEM* #
"################################################
"create content
LOOP AT fv_itab INTO fv_struc.
CLEAR: wa_file.
DO lv_lines TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE fv_struc TO <fs_cont>.
MOVE <fs_cont> TO lv_cont.
CONCATENATE wa_file '"' lv_cont '"' ',' INTO wa_file.
ENDDO.
"delete last seperator of content
lv_strlen = STRLEN( wa_file ).
lv_off = lv_strlen - 1.
REPLACE SECTION OFFSET lv_off LENGTH 1 OF wa_file WITH ''.
"append content to itab
APPEND wa_file TO it_file.
ENDLOOP.
"BREAK-POINT.
ENDFORM. " CREATE_OUTPUT
*&---------------------------------------------------------------------
*& Form get_structure
*&---------------------------------------------------------------------
FORM get_structure TABLES l_fields_table USING struc .
DATA:
l_oref_structure TYPE REF TO cl_abap_structdescr.",
"l_fields_table TYPE TABLE OF rfc_fields.
l_oref_structure ?= cl_abap_typedescr=>describe_by_data( struc ).
PERFORM build_field_table TABLES l_fields_table
USING l_oref_structure.
ENDFORM. " GET_STRUCTURE
*&---------------------------------------------------------------------
*& Form build_field_table
*&---------------------------------------------------------------------
FORM build_field_table TABLES pc_tab_fields STRUCTURE rfc_fields
USING value(pi_oref_structure)
TYPE REF TO cl_abap_structdescr.
DATA l_tabname TYPE dd02l-tabname.
DATA l_component TYPE abap_compdescr.
DATA l_field TYPE rfc_fields.
DATA l_offset TYPE i.
SEARCH pi_oref_structure->absolute_name FOR '\TYPE='.
IF sy-subrc = 0.
sy-fdpos = sy-fdpos + STRLEN( '\TYPE=' ) .
l_tabname = pi_oref_structure->absolute_name+sy-fdpos.
ELSE.
l_tabname = 'UNKNOWN'.
ENDIF.
CLEAR l_offset.
l_field-tabname = l_tabname.
LOOP AT pi_oref_structure->components INTO l_component.
MOVE-CORRESPONDING l_component TO l_field.
l_field-fieldname = l_component-name.
l_field-exid = l_component-type_kind.
l_field-intlength = l_component-length.
l_field-position = sy-tabix.
l_field-offset = l_offset.
l_offset = l_offset + l_field-intlength.
APPEND l_field TO pc_tab_fields.
ENDLOOP.
ENDFORM. " BUILD_FIELD_TABLE
Thanks in advance.
07-07-2008 4:36 PM
hi Toni,
do this way ...The structure of source and target field has to be same ....
data : lv_cont type fv_struc.
ASSIGN COMPONENT sy-index OF STRUCTURE fv_struc TO <fs_cont>.
IF SY-SUBRC = 0.
MOVE <fs_cont> TO lv_cont.
CONCATENATE wa_file '"' lv_cont '"' ',' INTO wa_file.
ENDIF.
07-07-2008 5:32 PM
Hi Santosh,
thanks for the fast answer, but this doesn't work. It is not possible to concatenate a structure.
Error message:
"LV_CONT" must be a character-type data object (data type C, N, D, T or STRING) .
That's the reason why I want to put the value of the current field into a char-variable.
Thanks and regards,
Toni
07-07-2008 5:57 PM
Hi,
Why don't you simple change your type definition, so that it only contains char like types? "ASSIGN COMPONENT ... " won't work as you think with non char like types. Refer to the help of the statement.
Regards,
Daniel
07-07-2008 6:06 PM
Hi Daniel,
I want the report to be able to create dynamically a csv file from every flat structure, so that you don't have to change the concatenate statement.
e.g.
Concatenate output '"' wa-field1 '",'
'"' wa-field2 '",'
'"' wa-field3 '"' into output.
now i change my structure and i add one field.. --> i have to change the report.
Concatenate output '"' wa-field1 '",'
'"' wa-field2 '",'
'"' wa-field3 '",'
'"' wa-field4 '"' into output.
-
I found a solution for this problem:
Data: lv_string type string,
lv_char type c length 255.
*first move <fs> to string
move <fs> to lv_string.
*next step: write the string into the char.
write lv_string to lv_char.
This works fine.
Regards,
Toni
07-07-2008 6:09 PM
Solution:
First move the field-symbol to a string variable. After that you can write the string variable to a char variable.
e.g.
DATA: lv_string TYPE string,
lv_char TYPE c length 255.
FIELD-SYMBOLS: <fs> TYPE ANY.
...
MOVE <fs> TO lv_string.
WRITE lv_string TO lv_char.