[Q] Matrix and Files

Ken Collins kenncoll123 at hotmail.com
Wed Dec 18 14:10:18 UTC 2002


Göran,

Thanks - the snippets indeed got me going. I found what I needed yesterday 
to read in a text file (at the Squeak Cookbook Site) the way I wanted and 
put it in a Matrix. I usually do search for answers on the Swikis and all 
the Documentation I have - sometimes I just don't find what I need, or may 
not recognize it as such by the way it is presented. The following works 
perfect:

| dir newStream m|
dir _ FileDirectory on: 'C:\WINDOWS\Desktop\GoodSqueak\New Folder'.
[newStream _ dir newFileNamed: 'TheWellBehavedMatrix.txt'.
m_TxTFromFile createFromFile:'GoodTxT.txt'."Create the Matrix"
1 to:12 do:
[:e | (m atRow:e)
do: [:val |
	newStream nextPutAll: val asString; nextPut: Character space ].
	newStream nextPutAll: String crlf]]
	ensure: [newStream close]

Thanks again,
Ken


>Well, I am not sure what kind of object you have containing the data but
>this snippet should get you going:
>
>| dir newStream |
>dir _ FileDirectory on: 'C:\WINDOWS\'.
>newStream _ dir newFileNamed: 'TheWellBehavedMatrix.txt'.
>#((1 #e 3) (4 #f 6) (7 #g 9)) do: [:row |
>	row do: [:val |
>		newStream nextPutAll: val asString; nextPut: Character space ].
>	newStream nextPutAll: String crlf].
>newStream close
>
>A little bitmore robust variant using ensure :
>
>| dir newStream |
>dir _ FileDirectory on: 'C:\WINDOWS\'.
>[newStream _ dir newFileNamed: 'TheWellBehavedMatrix.txt'.
>#((1 #e 3) (4 #f 6) (7 #g 9)) do: [:row |
>	row do: [:val |
>		newStream nextPutAll: val asString; nextPut: Character space ].
>	newStream nextPutAll: String crlf]]
>	ensure: [newStream close]
>
>Notes:
>	- There are various ways of doing line endings, since you are on Win32
>I took the liberty of simply adding CR LF. There are special streams for
>this too, the CrLfFileStream which automatically uses the correct line
>endings etc.
>	- The code above adds a space after the last column too, up to the
>reader to fix that! :-)
>	- You can also create file streams directly without using a
>FileDirectory object, check the "instance creation" methods on the class
>side of class FileStream and friends.
>
>
> > I feel like I am running in circles misusing FileDirectory,
> > DosFileDirectory, FileStream, StandardFileStream, and others in all 
>variety
> > of ways :)
> >
> > Thanx for any pointers,
>
>No problem. Try learning to use the following tools in your further
>experiments:
>- The method finder, very useful
>- Alt-m, Alt-n, Alt-N (use shift)
>
>The last three are used by selecting a piece of text first. Alt-m finds
>implementors of a message name, Alt-n finds senders of the same, Alt-N
>finds references to the class with the selected name. Alt-N is also
>available in the browser, select the class and use the popupmenu to find
>"class refs". This last one is very handy to find sample code in the
>image for using specific classes.
>
>regards, Göran


_________________________________________________________________
MSN 8 with e-mail virus protection service: 2 months FREE* 
http://join.msn.com/?page=features/virus




More information about the Squeak-dev mailing list