How to resolve the algorithm Associative array/Iteration step by step in the Oberon-2 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Associative array/Iteration step by step in the Oberon-2 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 Oberon-2 programming language

Source code in the oberon-2 programming language

MODULE AssociativeArray;
IMPORT
  ADT:Dictionary,
  Object:Boxed,
  Out;
TYPE
  Key = STRING;
  Value = Boxed.LongInt;
  
VAR
  assocArray: Dictionary.Dictionary(Key,Value);
  iterK: Dictionary.IterKeys(Key,Value);
  iterV: Dictionary.IterValues(Key,Value);
  aux: Value;
  k: Key;
  
BEGIN
  assocArray := NEW(Dictionary.Dictionary(Key,Value));
  assocArray.Set("ten",NEW(Value,10));
  assocArray.Set("eleven",NEW(Value,11));
  
  aux := assocArray.Get("ten");
  Out.LongInt(aux.value,0);Out.Ln;
  aux := assocArray.Get("eleven");
  Out.LongInt(aux.value,0);Out.Ln;Out.Ln;
  
  (* Iterate keys *)
  iterK := assocArray.IterKeys();
  WHILE (iterK.Next(k)) DO
    Out.Object(k);Out.Ln
  END;
  
  Out.Ln;
  
  (* Iterate values *)
  iterV := assocArray.IterValues();
  WHILE (iterV.Next(aux)) DO
    Out.LongInt(aux.value,0);Out.Ln
  END
  
END AssociativeArray.

  

You may also check:How to resolve the algorithm Isqrt (integer square root) of X step by step in the J programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the Factor programming language
You may also check:How to resolve the algorithm Nth root step by step in the Maple programming language
You may also check:How to resolve the algorithm Disarium numbers step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the REXX programming language