s LearnSqueak.com

ABSOLUTE BEGINNER

Your first Association: a label and a value

Make a labeled pair and read its two parts in five tiny Smalltalk steps.

5 minutes · 5 small steps · Squeak 6.1

STEP 1

Make a labeled pair

Open a Workspace from Squeak’s desktop menu: open… then workspace. Type this line, select it, right-click and choose print it (p). Some setups use middle-click.

The arrow -> joins a label to a value. This pair is called an Association.

Select the whole line → print it (p)
'score' -> 7
NATIVE SQUEAK CAPTURE
Native Squeak Workspace showing 'score' -> 7 with selected printed answer 'score'->7.
The real Workspace Print it command answers 'score'->7.
You should see

'score'->7

STEP 2

Read the label

Replace ALL the old text, including its printed answer. Type this line, select it and Print it.

key asks for the label on the left. Parentheses keep the pair together.

Select the whole line → print it (p)
('score' -> 7) key
NATIVE SQUEAK CAPTURE
Native Squeak Workspace showing ('score' -> 7) key with selected printed answer 'score'.
The real Workspace Print it command answers 'score'.
You should see

'score'

STEP 3

Read the value

Replace all the text again, then select this line and Print it.

value asks for the item on the right. The label and value have different jobs.

Select the whole line → print it (p)
('score' -> 7) value
NATIVE SQUEAK CAPTURE
Native Squeak Workspace showing ('score' -> 7) value with selected printed answer 7.
The real Workspace Print it command answers 7.
You should see

7

STEP 4

Use words as the value

Replace all the text, select this line and Print it.

A value need not be a number. Single quotes make text, so this pair links a pet label to a name.

Select the whole line → print it (p)
('pet' -> 'Milo') value
NATIVE SQUEAK CAPTURE
Native Squeak Workspace showing ('pet' -> 'Milo') value with selected printed answer 'Milo'.
The real Workspace Print it command answers 'Milo'.
You should see

'Milo'

STEP 5

Calculate a value

Replace all the text, select this line and Print it.

The parenthesized addition runs first. The pair receives 10 as its value; value then reads it.

Select the whole line → print it (p)
('score' -> (7 + 3)) value
NATIVE SQUEAK CAPTURE
Native Squeak Workspace showing ('score' -> (7 + 3)) value with selected printed answer 10.
The real Workspace Print it command answers 10.
You should see

10

YOU DID IT

Two parts. One useful pair.

An Association keeps a key and a value together. The arrow makes the pair; key and value read its parts.

Change 'score' to 'lives' and 7 to 3. Ask for key, then value. Replace the old printed answer each time. This is one pair, not a whole Dictionary.

Optional code download: open it as text and copy one example into Workspace.

Five Association examples: copy one at a time (.st)

Keep several labeled values in a Dictionary · Try text messages · Official Squeak documentation