File I/O syntax

I’m trying to write a matrix like this

1111b

1010b

1100b

1000b

to a text file (actually to a .ppm or .pgm file) using the function below. No matter how many variations on the file I/O syntax shown in Q4M I try, I keep getting syntax errors. I must be doing something obviously wrong, but I can’t see it. What am I doing wrong?

wrtimg:{

r:count x;

h:hopen: `:c:/Documents and Settings/Stu/Desktop/fig.txt;

(neg h) [“P2”];

(neg h) [“#fig.txt”];

(neg h) (string r)," ",string r;

(neg h) [“255”];

x:255*raze abs x;

n:count x;

i: 0;

do [

n ;

(neg h) string x[i];

i:i+1];

hclose h}


With this function and the 4x4 matrix above I want to get


P2

#fig.txt

4 4

255


followed by 16 integer values, one per line.

Hello Stuart,
Here I dont think you need any loop.I did following and it worked.

q)h:hopen `:abc.txt

q)(neg h)“P2”

-384i

q)(neg h)“#fig.txt”

-384i

q)(neg h)string[count[a]]," ",string count a

-384i

q)(neg h)“255”

-384i

q)a:255*raze abs a

q)(neg h)each string a

-384 -384 -384 -384 -384 -384 -384 -384 -384 -384 -384 -384 -384 -384 -384 -384i

q)read0 `:abc.txt

“P2”

#fig.txt”

“4 4”

“255”

,“0”

“255”

,“0”

“255”

,“0”

“255”

,“0”

“255”

,“0”

“255”

,“0”

“255”

,“0”

“255”

,“0”

“255”

Hope this helps.

Thanks

Showvik 

Showvik:

Yes, you nailed it! This group is awesome. I get working solutions right after I post a problem.

-Stu S.

=====