Another question about Export/Put

Jansen

New Member
I am writing to a text file delimited by a "|". I have used the export command

export delimiter "|"
field 1
field 2
...

However this does put quotes around some of the data because of standard form. I went ahead and used a put statement but unfortunately this becomes anoying when a "|" has to go after every field. Becomes tedious when you have 135 fields.

put stream Name unformatted
field 1 "|"
field 2 "|"
...

My question, is there a way to use the EXPORT function so the quotes are not there or is there a way to use the PUT funtion so I do not have to put in all those "|". Or maybe there is another way? Thanks.
 
You could try to do it with a dynamic query:

Code:
DEFINE INPUT  PARAMETER cp_table AS CHARACTER  NO-UNDO.
DEFINE INPUT  PARAMETER cp_delimiter AS CHARACTER  NO-UNDO.
 
DEFINE VARIABLE h_QUERY AS HANDLE     NO-UNDO.
DEFINE VARIABLE h_buffer AS HANDLE     NO-UNDO.
DEFINE VARIABLE l_ok AS LOGICAL    NO-UNDO.
DEFINE VARIABLE i_numfields AS INTEGER    NO-UNDO.
DEFINE STREAM s_out.
 
OUTPUT STREAM s_out TO c:\temp\data.out.
 
CREATE QUERY h_query.
CREATE BUFFER h_buffer FOR TABLE cp_table.
 
i_numfields = h_buffer:NUM-FIELDS.
 
h_query:ADD-BUFFER(h_buffer).
l_ok = h_query:QUERY-PREPARE("for each " + cp_table + " NO-lOCK").
IF l_ok
THEN DO:
    h_query:QUERY-OPEN().
    h_query:GET-FIRST().
    DO WHILE NOT h_query:QUERY-OFF-END:
        RUN ExportData.
        h_query:GET-NEXT().
    END.
END.
 
OUTPUT STREAM s_out CLOSE.
/* cleanup */
DELETE OBJECT h_buffer.
DELETE OBJECT h_query.
 
PROCEDURE ExportData:
DEFINE VARIABLE c_put AS CHARACTER NO-UNDO.
DEFINE VARIABLE i_tel AS INTEGER    NO-UNDO.
 
DO i_tel = 1 TO i_numfields:
     c_put = c_put + STRING(h_buffer:BUFFER-FIELD(i_tel):BUFFER-VALUE) +     cp_delimiter.
END.
 
ASSIGN c_put = TRIM(c_put,cp_delimiter).
PUT STREAM s_out UNFORMATTED c_put SKIP.
 
END PROCEDURE.

You just have to input a table name and a delimiter and the output is generated. Just wrote it so you probably need to tweek it regarding formats (now I string everything) and perhaps query.

But I hope you get the idea.

Regards,

Casper.
 
Jansen said:
I went ahead and used a put statement but unfortunately this becomes anoying when a "|" has to go after every field. Becomes tedious when you have 135 fields.

put stream Name unformatted
field 1 "|"
field 2 "|"
...
On occasion in the past I have cheated in similar situations by pre-editing the .p in Excel. In your case, after you have created the field-name column, the delimiter column would take about 2 secs to replicate, then you could paste the rwo into your .p.
 
Your way is much better if all the fields come from a table, and may vary at runtime.

Mine is an edit-time-only kludge that is a workaround when editing code that contains extended columns. eg. the above, and API constant lists. But it works. :blush:
 
What I tend to do in cases such as these, when I know how many fields are going to be needed, is to create a temp-table with a counter index and a data field that is an array of strings.

It may not be as elegant as a dynamic temp-table, but it works for V7/V8 as well as V6 (if you use a workfile rather than a temp-table).

Simon


def temp-table t_table no-undo
field counter as int
field mydata as char extent 200
index idx1 counter.

def var this_loop as int no-undo.
def var howevermanyfieldsiwant as int no-undo. /* Set this to be the number of fields required */

/*

Populate t_table with whatever values you want from various tables.

*/

output to c:\myfile.d.
for each t_table no-lock:
do this_loop = 1 to howevermanyfieldsiwant:
put unformatted t_table.mydata[this_loop] "|".
end.
put unformatted skip.
end.
output close.
 
Back
Top