03-02-2006 9:26 PM
Hi,
I'm trying to write the contents of an internal table to the application server, this is my code
OPEN DATASET txt_out
FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc = 0.
LOOP AT it_global_data INTO wa_global_data.
TRANSFER wa_global_data TO txt_out.
ENDLOOP.
WRITE: / 'data was written to ', txt_out.
ELSE.
WRITE: / 'Error, sy-subrc is: ', sy-subrc.
ENDIF.
CLOSE DATASET txt_out.
Now, I want to do 2 things:
1) Remove all the unnecessary spaces after each field (is there a function to do that?)
2) Between each field I want to have a ';' in my output file
The trick is performance is a big issue because the file can be huge (200,000+ lines) so I don't think parsing each field and manually placing a ';' after it will be very fast.
03-03-2006 7:15 AM
Hi,
Try following code..
data : lv_string(1000).
OPEN DATASET txt_out
FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc = 0.
LOOP AT it_global_data INTO wa_global_data.
concatenate wa_global_data-field1
wa_global_data-field2
.
.
wa_global_data-fieldn
into lv_string separated by ','.
condense lv_string no-gaps.
*TRANSFER wa_global_data TO txt_out.
TRANSFER lv_string to txt_out.
ENDLOOP.
WRITE: / 'data was written to ', txt_out.
ELSE.
WRITE: / 'Error, sy-subrc is: ', sy-subrc.
ENDIF.
CLOSE DATASET txt_out.
All the fileds of wa_global_data should be of type c or string.
If not, then use the FM 'SO_STRUCT_TO_CHAR'
to convert it into charecter string and use above logic.
Hope this will help.
Regards,
Shashank
03-02-2006 9:36 PM
Try:
LOOP AT it_global_data INTO wa_global_data.
condense wa_global_data.
TRANSFER wa_global_data TO txt_out.
ENDLOOP.
This will get rid of most spaces, but leave one between fields.
Rob
03-02-2006 9:42 PM
Nice, it works great! Now all I need is to put a ';' between each field, is there a quick way to do it?
Thanks.
03-02-2006 9:42 PM
When you are writing the file out, you are already doing a loop. All you have to do is this.
Define another variable that has a length as that of the internal table record, say mystring(1000). Then inside your loop, all you have to do is
LOOP AT it_global_data INTO wa_global_data.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE wa_global_data TO <fs>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
CONCATENATE mystring
<fs>
INTO mystring SEPARATED BY SPACE.
ENDDO.
CONDENSE mystring.
TRANSFER mystring TO txt_out.
ENDLOOP.
03-02-2006 9:44 PM
Hi
1) No! I don't think because the problem is how to know which spaces are unnecessary.
You can try to do this:
MOVE wa_global_data TO STRING.
CONDENSE STRING NO-GAPS.
TRANSFER STRING TO <FILE>.
But in this way all spaces'll be removed.
2) Try to use the fm SAP_CONVERT_TO_CSV_FORMAT: but this fm check the structure of your file and after each field insert ';', so I don't know if it's what you would like to do.
If you don't want to insert "manually" ';' you should prepare the structure before transfering the data to file.
For example if my structure is:
DATA: BEGIN OF ST_DATA,
FIELD1,
FIELD2,
.......
FIELDN,
END OF ST_DATA.
The structure for file should be:
DATA: BEGIN OF ST_FILE,
FIELD1,
FILL1,
FIELD2,
FILL2,
.......
FIELDN,
FILLN,
END OF ST_FILE.
LOOP AT it_global_data INTO ST_DATA.
CLEAR ST_FILE WITH ';'.
MOVE-CORRESPONDING ST_DATA TO ST_FILE.
CONDENSE ST_FILE NO-GAP.
TRANSFER ST_FILE TO <FILE.
ENDLOOP.
Max
03-02-2006 10:02 PM
Thanks for your help guys, the SAP_CONVERT_TO_CSV_FORMAT technique takes too long on my table so unfortunately I can't use it.
I will try preparing my structure with the FILLs and then doing a condense to get rid of the spaces.
03-03-2006 12:40 AM
You can do like this:
Data: begin of int. table,
field1,
filler1 value ';',
field2,
filler2 value ';',
...
...
end of itn. table.
after populating int. table, use
condense int. table no-gaps.
Now you can directly transfer the int. table
condense
03-03-2006 5:23 AM
Hi,
You can first remove all spaces using CONDENSE and then replace the single spaces left with TRANSALTE.
A sample code is given below:
REPORT ZTMP .
DATA: WA_TXT(100) VALUE 'THE MAGIC OF TRANSLATE',
WA_CHANGE(2) VALUE ' ;',
WA_LTH TYPE I.
CONDENSE WA_TXT.
* Get length of actual record after condensing
WA_LTH = STRLEN( WA_TXT ).
TRANSLATE WA_TXT USING WA_CHANGE.
* Delete trailing ; from the record
WA_TXT = WA_TXT+0(WA_LTH).
WRITE: / WA_TXT.
Since this code is relatively simple, it should not result in performance issues.
Hope this helps,
Regards,
Madhur
NB: Please award points if found helpful
03-03-2006 7:15 AM
Hi,
Try following code..
data : lv_string(1000).
OPEN DATASET txt_out
FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc = 0.
LOOP AT it_global_data INTO wa_global_data.
concatenate wa_global_data-field1
wa_global_data-field2
.
.
wa_global_data-fieldn
into lv_string separated by ','.
condense lv_string no-gaps.
*TRANSFER wa_global_data TO txt_out.
TRANSFER lv_string to txt_out.
ENDLOOP.
WRITE: / 'data was written to ', txt_out.
ELSE.
WRITE: / 'Error, sy-subrc is: ', sy-subrc.
ENDIF.
CLOSE DATASET txt_out.
All the fileds of wa_global_data should be of type c or string.
If not, then use the FM 'SO_STRUCT_TO_CHAR'
to convert it into charecter string and use above logic.
Hope this will help.
Regards,
Shashank