Squeak Beginner

Göran Hultgren gohu at rocketmail.com
Mon Nov 5 21:34:43 UTC 2001


Hi Tamika and gang!

--- "Knox, Tamika" <tknox at ajilon.com> wrote:
> Hi all!
> 
> I posted a dilemma here over a month ago and Alan Kay was gracious enough to
> create the following tutorial on the Swiki for us:
> <http://squeakland.org:8080/super/200> http://squeakland.org:8080/super/200
> 
> There are three of us who have never used Squeak and we are still having
> some problems.  In our last group meeting for this project, we had the
> following concerns:
> 
> Based on the above tutorial, instead of creating morphic projects for each
> country (there are over 200+ countries), how can we go about having one
> single "template" for each country and pull the appropriate data from an
> external file as needed since the same data will be required more than once?
> For example, we will need to pull the same data for our tutorial section.
> 
> Also, if we created external text files with the data (i.e. country name -
> primary key, continent name - secondary key, etc) separating the "record,"
> if you will (I'm used to programming in Pascal), by line, how does Squeak
> process the data?

Well there are as usual tons of possible solutions. :-) As often before on this list we try not to
just give solutions but try to teach *how to find* a solution!

Ok, the problem is file reading.
Keywords: file, read, line
Tools: MethodFinder, Browser

Let's do it with the Browser, in the first pane we have the "find class..." function which can
take name fragments as input. We type "file" and get a whole bunch of hits. Whuff!

If we take a look we might conclude that FileStream and FileDirectory sounds general enough, the
others seem rather specific to different areas. (These two are the basic file handling classes in
Squeak)

Ok, let's take a look at FileStream and I always start with looking at the class comment.
Hmmm, well we learn a bit more but it actually could be improved with some simple example.
Next trick: Bring up the popupmenu for FileStream and choose "class refs" - this will show all
methods in the image that uses the class explicitly (thus probably instantiating the class).

Ouch, a lot of methods... letting my eyes flow over the methods I find one starting with the word
"read"... might be good: EnvelopeEditorMorph>>readFileNamed:

readFileNamed: fileName
	| snd |
	snd _ Compiler evaluate:
		(FileStream readOnlyFileNamed: fileName) contentsOfEntireFile.
	soundName _ fileName copyFrom: 1 to: fileName size-4. "---.fmp"
	self editSound: snd

He! This method actually uses a neat trick. First we learn that this simple expression reads in a
full file as a String in memory in one swoop:

(FileStream readOnlyFileNamed: fileName) contentsOfEntireFile

And then we hand that String over to the Compiler that evaluates it? Freaky! Not something you
could do in Pascal for sure! Ok, so what can that file contain then? Actually anything you could
type in a workspace for example:

#(
(1 2 'a string' aSymbol)
(3 4 'another string' aSymbol (anArray 1 2))
)

Note: Using the array notation you don't include the #-sign for elements within the array for
embedded arrays or symbols.

If you create a file called say 'test' with the above content (in your Squeak directory) and then
inspect the following code in a workspace:

Compiler evaluate: (FileStream readOnlyFileNamed: 'test') contentsOfEntireFile

Tada! An array as expected! 

Ok, so this is probably the absolutely shortest solution that takes the "shortcut" by reusing the
Compiler for getting a syntax for representing data - the same syntax we use for literals and code
in Smalltalk. Pretty neat.

Of course you could do a normal line-by-line-parse-thingy if you want... :-)

> If anyone has a solution, we'll be glad to hear it. Also, any Smalltalk or
> Squeak links that will assist us with data handling will be greatly
> appreciated.

Well, I don't know but the Swiki might have other good examples.

regards, Göran


=====
Göran Hultgren, goran.hultgren at bluefish.se
GSM: +46 70 3933950, http://www.bluefish.se
"Department of Redundancy department." -- ThinkGeek

__________________________________________________
Do You Yahoo!?
Find a job, post your resume.
http://careers.yahoo.com




More information about the Squeak-dev mailing list