[Oberon] How to mimic an associative array

Jörg Straube joerg.straube at iaeth.ch
Fri Dec 3 07:33:09 CET 2010


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');


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

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.
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.

Merry xmas!
Joerg

-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.inf.ethz.ch/pipermail/oberon/attachments/20101203/ccc5b99b/attachment.html 


More information about the Oberon mailing list