[Oberon] How to mimic an associative array

Duke Normandin dukeofperl at ml1.net
Sat Dec 4 15:39:52 CET 2010


On Fri, 3 Dec 2010, Jörg Straube wrote:

> Hi
>
> I followed the discussion for a while, and up to now saw
> TWO different solutions to your original question:
>
> 1) proposed by Sven
>   TYPE
>     AssociativeArray = OBJECT
> 	PROCEDURE get(key : String) : String;
> 	PROCEDURE put(key, value : String);
> 	PROCEDURE remove(key : String);
>     END AssociativeArray;
>
> 2) proposed by Aubrey
>   TYPE
>      myHashPtr = POINTER TO myHash;
>
>   PROCEDURE Init* (VAR t : myHash);
>   PROCEDURE Enter* (VAR t: myHash, key: keyType, value: valueType);
>   PROCEDURE This* (t : myHash, key: keyType, VAR valueType);
>   PROCEDURE Remove* (VAR t: myHash, key : keyType);
>   PROCEDURE Enumerate* (t : myHash, handler : someProcType);
>
> Aubrey took more the "classical" procedural approach à la Pascal or
> Modula.  Sven proposed the object approach possible in Oberon.
>
> Personally, I like Sven's approach more. (In Sven's mail you even
> find a complete Oberon implementation for his OBJECT). But you can
> see that there are different approaches to model and implement a
> given problem or data structure.
>
> In other languages you write
>   capital['england'] = 'london'
>   location = capital['england']
>
> With Sven's approach you would write
>   VAR capital: AssociativeArray;
>   capital.put('england', 'london');
>   location := capital.get('england');
>

Thank you for clarifying two approaches to a solution for my
self-imposed project _and_ the comparison to how it would be done in
other languages. I now can _see_ the analogy.

I re-read the Oberon-2 Report (again!), as well as the "Active Oberon
Language Report", by Patrik Reali. I see, that in order to apply
Sven's approach, my compiler has to support the Active Oberon
extensions. :( Sadly the tool-chain that I use (and prefer at the
moment) support Oberon-2 only. To use Sven's method, I suppose I would
have to use A2 as my programming environment. Or Component Pascal
maybe?

We mustn't forget Doug Danforth's approach to clarifying my confusion
:) I think that his last post on the matter offers a solution that
could be achieved using Oberon-2, although I yet to work out the
details of how it would work. That's on the "to do" list.

> Associative Arrays are called differently in different programming
> languages
> - "dictionary" in e.g. Python and Objective-C
> - "hash" in  e.g. Perl and Ruby
> - "map" in e.g. C++ and Java

Alleluia!! I feel like Crash (or was it Eddy?) in the movie "Ice Age
II" when he wakes up after smashing into a tree from being "launched"
by Manny. :D

> I guess the wording "hash" used by Perl generated some confusion, as
> in computer science a "hash" normally describes a mapping function
> to a countable set of values.

I see (saw) that ...

> Others already pointed out that an associative array can be
> implemented using a hash function. BTW Sven used (a very simple)
> hash function in his proposed implementation. Have a look at his
> mail, it gives quite some good insight in how to program in Oberon.

Should that be "good insight in how to program in 'Active' Oberon"?

> Merry xmas!

and may the blessings of Christmas be on you and yours as well!
Much obliged...
-- 
Duke


More information about the Oberon mailing list