How to resolve the algorithm Associative array/Creation step by step in the Visual FoxPro programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Associative array/Creation step by step in the Visual FoxPro programming language

Table of Contents

Problem Statement

The goal is to create an associative array (also known as a dictionary, map, or hash).

Related tasks:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Associative array/Creation step by step in the Visual FoxPro programming language

Source code in the visual programming language

LOCAL loCol As Collection, k, n, o
CLEAR 
*!* Example using strings
loCol = NEWOBJECT("Collection")
loCol.Add("Apples", "A")
loCol.Add("Oranges", "O")
loCol.Add("Pears", "P")
n = loCol.Count
? "Items:", n
*!* Loop through the collection
k = 1
FOR EACH o IN loCol FOXOBJECT
    ? o, loCol.GetKey(k)
    k = k + 1 
ENDFOR	
*!* Get an item by its key
? loCol("O")
?
*!* Example using objects
LOCAL loFruits As Collection
loFruits = NEWOBJECT("Collection")
loFruits.Add(CREATEOBJECT("fruit", "Apples"), "A")
loFruits.Add(CREATEOBJECT("fruit", "Oranges"), "O")
loFruits.Add(CREATEOBJECT("fruit", "Pears"), "P")
*!* Loop through the collection
k = 1
FOR EACH o IN loFruits FOXOBJECT
    ? o.Name, loFruits.GetKey(k)
    k = k + 1 
ENDFOR
*!* Get an item name by its key
? loFruits("P").Name


DEFINE CLASS fruit As Custom
PROCEDURE Init(tcName As String)
THIS.Name = tcName
ENDPROC
ENDDEFINE

  

You may also check:How to resolve the algorithm Primality by trial division step by step in the Python programming language
You may also check:How to resolve the algorithm Ray-casting algorithm step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Tau number step by step in the Python programming language
You may also check:How to resolve the algorithm Josephus problem step by step in the XPL0 programming language