Clusters arrays can be used to create high-speed translation tables. Every field of a
cluster can be used as
a lookup key. The
findrow() function is used to do cluster key lookups. All data types
are supported. Duplicate keys are supported as well
Findrow() is highly optimized and can
do over 10 million searches per second. This makes findrow() ideal for
tasks that require fast lookups. Given the cluster name, cluster field to search
in, and data to searched for, findrow() returns the first row where
the key was found or returns a 0 if the key was not found.
To handle
duplicate keys, findrow() has an optional third parameter to find the Nth occurrence of a key.
- All fields of a cluster, regardless of data type, are searchable as a key
- All keys are changeable
- All keys can contain duplicates
- All key searches take O(1) constant time -- virtually instant lookups
By default, all searches are case-regardless. Set the optional fourth parameter to TRUE if
you want a case-sensitive search.
Because all cluster fields are keys, bi-directional lookups are easy to do. In our example we will translate
from English to German and German to English.
To define a cluster, use the
cluster statement followed by the name of the cluster. In
our example, we are calling our cluster
trans. Our trans cluster has two
fields:
english$ and
german$. To reference them we use
trans->english$
and
trans->german$.
cluster trans: english$, german$
The code below uses the
trans cluster to
translate from English to German and German to English.
// simple translation dictionary
cluster trans: english$, german$
add cluster trans: english$ = 'apple', german$ = 'der Apfel'
add cluster trans: english$ = 'banana', german$ = 'der Banane'
word$ = a$
do
// First, try translating English to German
row = findrow(trans->english$, word$)
if row > 0 then
print word$;' >> '; trans->german$
exit do
end if
// Not found as English, try German
row = findrow(trans->german$, word$)
if row > 0 then
print word$;' >> '; trans->english$
exit do
end if
print 'No translation for '; word$
end do
Program Explanation:
1. Cluster Definition:
A cluster named trans
is defined to store English words and their corresponding German translations. The cluster has two fields: english$
for English words and german$
for German words.
2. Adding Data:
The program adds entries to the trans
cluster:
- The English word
'apple'
is paired with the German word 'der Apfel'
.
- The English word
'banana'
is paired with the German word 'die Banane'
.
3. User Input Assignment:
The variable word$
is assigned the value of a$
, which is assumed to be a word entered by the user that they want to translate.
4. Searching for English Word:
The program searches the trans
cluster for a row where the english$
field matches word$
(the word entered by the user). The row number is stored in the variable row
.
5. English to German Translation:
If the word is found (row > 0
), the program prints the English word followed by its German translation, using ' >> '
as the separator. The loop then exits since the translation has been found.
6. Searching for German Word:
If the word wasn't found in the English field, the program then searches the trans
cluster for a row where the german$
field matches word$
.
7. German to English Translation:
If the word is found in the German field (row > 0
), the program prints the German word followed by its English translation, again using ' >> '
as the separator. The loop then exits since the translation has been found.
8. No Translation Found:
If the word is not found in either the English or German fields, the program prints a message indicating that there is no translation available for the word entered by the user.
Summary:
The program is a simple translation tool that allows the user to
translate words between English and German. It first tries to translate
the word from English to German. If the word isn't found in the English field,
it then tries to translate it from German to English. If the word is not found
in either language, it notifies the user that there is no translation available.