[Q] Matrix and Files

goran.hultgren at bluefish.se goran.hultgren at bluefish.se
Wed Dec 18 09:07:11 UTC 2002


"Ken Collins" <kenncoll123 at hotmail.com> wrote:
> Hi everyone,
> First, I want to say thanks for the help I have gotten from this list. 
> Second, I will likely be asking a lot of questions in the near future - and 
> although I suppose this is the THE place to ask Squeak questions, I 
> sometimes feel awkward doing so... maybe like there is some limit to how 
> many you can ask or something... maybe it's just the weather :)

Hehe, you can always ask. Some newbies tend to ask and ask without doing
the "homework" - for example if we give hints on how to learn something
or where to find reading material then we expect that you also at least
try to follow the advice - if you do that and still fails, noone will
blame you for asking more! But that is just common sense, right?

In general people on this list like to help out.

> Anyway - if I have made a Matrix in a Workspace with the following 
> rows/columns:
> 
> 0 3 2 1 6 5 4 9 8 7 e t
> 9 0 e t 3 2 1 6 5 4 8 7
> t 1 0 e 4 3 2 7 6 5 9 8
> e 2 1 0 5 4 3 8 7 6 t 9
> 6 9 8 7 0 e t 3 2 1 5 4
> 7 t 9 8 1 0 e 4 3 2 6 5
> 8 e t 9 2 1 0 5 4 3 7 6
> 3 6 5 4 9 8 7 0 e t 2 1
> 4 7 6 5 t 9 8 1 0 e 3 2
> 5 8 7 6 e t 9 2 1 0 4 3
> 1 4 3 2 7 6 5 t 9 8 0 e
> 2 5 4 3 8 7 6 e t 9 1 0
> 
> how do I put it out into a text file that will look exactly like this when I 
> open it (with notepad or word)? Something/somewhere  like: 
> 'C:\WINDOWS\Desktop\GoodSqueak\TheWellBehavedMatrix.txt'. I want the file to 
> have a space between each row element, and 1 row per line.

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




More information about the Squeak-dev mailing list