Re: WriteList function?
- To: mathgroup at smc.vnet.net
- Subject: [mg9329] Re: [mg9320] WriteList function?
- From: jpk at max.mpae.gwdg.de
- Date: Sat, 1 Nov 1997 03:33:18 -0500
- Sender: owner-wri-mathgroup at wolfram.com
Hi Tom, with the definitions writeList[stream_,l_List,numsep_String]:= Write[stream,Infix[l,numsep]] writeMatrix[stream_,mat_?MatrixQ,numsep_:" "]:= Scan[writeList[stream,#,numsep]&,mat] You can say writeMatrix["stdout",{{1,2,0.5,0.8}, {7,1,2,4}, {0.5,1,1,2}}," "] to get the matrix on screen in the way You want. And You can send it on a open stream in this way by replacing the first argument of writeMatrix[] with a open output stream. Hope that helps Jens > Hi, I am a new Mathematica user. > > I have had success reading files of space-separated numbers using > ReadList, but now need to output my results in the same fashion. i.e. > one row of the matrix should appear on one text line, > no commas, nor curly braces are desired in the output. > > I have searched the manual for a corresponding WriteList function > without success. > > Any suggestions? I may have to resort to awk/sed/perl to postprocess > the file, but I would've thought Mathematica would've provided the > facilities to write data in a general format. > > Thank you kindly, --tom thomas > ------------------------------------------------------------------- > example: > In[1]:= !! /tmp/mymat > 1 2 3 > 4 5 6 > 7 8 9 > > In[1]:= mydata = ReadList["/tmp/mymat", Number, RecordLists->True] > > Out[1]= {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} > > In[2]:= stmp = OpenWrite["/tmp/myoutput"] > > Out[2]= OutputStream[/tmp/myoutput, 4] > > In[3]:= Write[stmp, mydata] > > > %cat /tmp/myoutput > {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} > > I need to see instead: > 1 2 3 > 4 5 6 > 7 8 9 >