How to resolve the algorithm Associative array/Iteration step by step in the Smalltalk programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Associative array/Iteration step by step in the Smalltalk programming language

Table of Contents

Problem Statement

Also show how to iterate just over the keys, or the values, if there is a separate way to do that in your language.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Associative array/Iteration step by step in the Smalltalk programming language

Source code in the smalltalk programming language

|pairs|
pairs := Dictionary 
	    from: { 'hello' -> 1. 'world' -> 2. '!' -> 3. 'another!' -> 3 }.

"iterate over keys and values"
pairs keysAndValuesDo: [ :k :v |
    ('(k, v) = (%1, %2)' % { k. v }) displayNl
].

"iterate over keys"
pairs keysDo: [ :key |
    ('key = %1, value = %2' % { key. pairs at: key }) displayNl
].

"iterate over values"
pairs do: [ :value |
    ('value = %1' % { value }) displayNl
].

(pairs keys) do: [ :k | "..." ].
(pairs values) do: [ :v | "..." ].

  

You may also check:How to resolve the algorithm Greatest element of a list step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Abstract type step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Verify distribution uniformity/Chi-squared test step by step in the Phix programming language
You may also check:How to resolve the algorithm Rhonda numbers step by step in the Arturo programming language
You may also check:How to resolve the algorithm Isqrt (integer square root) of X step by step in the Quackery programming language