Question
James Woo · Mar 17

How to write "zwrite" output to $$$TRACE?

SET ^||fruit(1)="apple",^||fruit(4)="banana",^||fruit(8)="cherry"
SET ^||fruit(1,1)="Macintosh",^||fruit(1,2)="Delicious",^||fruit(1,3)="Granny Smith"
SET ^||fruit(1,2,1)="Red Delicious",^||fruit(1,2,2)="Golden Delicious"
SET ^||fruit="Fruits"
ZWRITE ^||fruit

^||fruit="Fruits"
^||fruit(1)="apple"
^||fruit(1,1)="Macintosh"
^||fruit(1,2)="Delicious"
^||fruit(1,2,1)="Red Delicious"
^||fruit(1,2,2)="Golden Delicious"
^||fruit(1,3)="Granny Smith"
^||fruit(4)="banana"
^||fruit(8)="cherry"


How I do send the output of zwrite to $$$TRACE()?

 

Product version: IRIS 2021.2
0
0 180
Discussion (4)2
Log in or sign up to continue

use the redirect package from Open Exchange to write it to string

https://openexchange.intersystems.com/package/IO-Redirect

tmpFile = ##class(%FileCharacterStream).%New()
myf = tmpFile.Filename = ##class(%File).TempFilename("txt")
open myf:("NW") USE myf ZWRITE ^||fruit
CLOSE myf
tLine = ""
tmpFile.Rewind()
while ('tmpFile.AtEnd) {
set tLine = tLine_tmpFile.ReadLine()
}
##class(%File).Delete(myf)
w tLine

I'm quite shure, the above code won't work as expected, or with the words of Joseph Weizenbaum: “A computer will do what you tell it to do, but that may be much different from what you had in mind.”

The content of your myf variable is always 0 (the result of comparing nullstring with a filename), the size of tmpFile stream is also 0 (you never write into the stream).

Sometimes it's faster to write a "oneliner" to solve a simple problem then searching and downloading a solution from openexchange or from whereever... That's the beauty of the ObjectScript.

And if you think, the oneliner is worth to be reused, then make it to a method, add some small adjustments for a general usability...

The oneliner

s str="",tmp=##class(%File).TempFilename("txt") o tmp:"NWRU":0 i $t { u tmp zw ^||fruit s s=$zpos r:'$zseek(0) str#s c tmp:"D" }

The more general version

ClassMethod ToString(ref,max=32000)
{
    s tmp=##class(%File).TempFilename("txt") o tmp:"NWRU":0 q:'$t ""
    u tmp zw @ref s siz=$zpos r:'$zseek(0) str#$s(siz>max:max,1:siz) c tmp:"D" q str
}

Use it as

write ##(your.class).ToString($na(^||fruit))


You could redirect output to anything convenient (spool device, interprocess communication device, host file) then read it back into a variable and $$$TRACE the variable.  E.g., a two-liner using spool device 1:

k str o 2 u 2 zw  s numLines=$za-1 c 2 f line=1:1:numLines s str=str_^SPOOL(1,line)
$$$TRACE(str)