Sheerpower Logo
D.4  Using Clusters as a programming dictionary
Many programming languages have a specialized data structure that acts as a Data Dictionary. With sheerpower, clusters can be used for this purpose.

The code below demonstrates using a Sherpower cluster as a Data Dictionary.

// An example of using a cluster as a simple Key/Value pair Data Dictionary // Note: All cluster variables can be used as keys cluster capitals: country$, city$ add cluster capitals capitals->country$ = 'USA' capitals->city$ = 'Washington, D.C.' add cluster capitals: capitals->country$ = 'Germany', capitals->city$ = 'Berlin' add cluster capitals: capitals->country$ = 'China', capitals->city$ = 'Beijing' row = findrow(capitals->country$, 'Germany') if row = 0 then print 'Unknown' else print capitals->city$ end if row = findrow(capitals->city$, 'Beijing') if row = 0 then print 'Unknown' else print capitals->country$ end if

Program Explanation:

1. Comments:

The comments explain the purpose of the program and highlight that any variable within a cluster can be used as a key to look up values.

2. Cluster Definition:

A cluster named capitals is defined to store country names and their corresponding capital cities. The fields in the cluster are country$ and city$.

3. Adding Data:

The program adds entries to the capitals cluster:

  • USA with the capital Washington, D.C.
  • Germany with the capital Berlin
  • China with the capital Beijing

4. Searching by Key:

The program searches the capitals cluster for the row where the country$ field matches Germany. If found, it prints the corresponding capital city.

5. Searching by Value:

The program then searches the capitals cluster for the row where the city$ field matches Beijing. If found, it prints the corresponding country name.

Summary:

This program demonstrates how to use a sheerpower cluster as a simple Key/Value data dictionary, where you can store and retrieve pairs of country names and their capital cities.

Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.
Wide screen