PerfectScript Journal

April 1998: Using Arrays

When you start using macros, you learn very quickly how handy macro
variables are. You can set values at the top of the macro, then
use their values throughout. If you ever need to change a value,
you can do it at the top of the macro, and all the references to
that variable are automatically updated.

There is another type of variable in WordPerfect macros called an
array. An array is a variable that can contain more than
one value. Each value has an index by which you can refer to it
in your macro. The index is specified in square brackets [].

The example below shows how to define values to an array, then use
them in another macro statement. You should be able to cut and
paste this code into your copy of WordPerfect, then compile and
run it to see the results.

Declare vArray[3]
vArray[1]:="Item 1"
vArray[2]:="Item 2"
vArray[3]:="Item 3"
MessageBox(; "Array Test"; vArray[1])
MessageBox(; "Array Test"; vArray[2])
MessageBox(; "Array Test"; vArray[3])
          

Notice the DECLARE statement at the beginning of the macro above.
We must always declare an array variable before assigning
values. PerfectScript needs to know how many elements will
be in the array so it can set aside space in memory for the
values.

You should have also noticed how we specified the index (1, 2, and 3)
of each value we assigned to the array. PerfectScript also
creates a 0 index value automatically. vArray[0] contains the
number of items in the array. This value is very useful,
especially of you are processing array values consecutively
in a FORNEXT loop.


Declare vArray[3]
vArray[1]:="Item 1"
vArray[2]:="Item 2"
vArray[3]:="Item 3"
Fornext(x; 1; vArray[0])  // vArray[0] = 3
 MessageBox(; "Array Test"; vArray[x])
Endfor
          

The arrays we have looked at so far are single-dimensional arrays,
meaning they have only one index value in the square brackets.
You can also create multidimensional arrays that have 2 or more
index values. If you have information that is related in some way,
multi-dimensional arrays are very handy when you need to organize
data efficiently.

In the example below, we have three pieces of information about some
particular animals: the type, breed and age associated with each
animal. So we create a two-dimensional array to organize this
information. Animal 1 is a four-year old siamese cat. Animal 2
is a 2 year old Poodle dog, and Animal 3 is a 14 year old Arabian
horse. We can organize the data like this:


Declare vArray[3,3]
vArray[1,1]:="Cat"
vArray[1,2]:="Siamese"
vArray[1,3]:="4"

vArray[2,1]:="Dog"
vArray[2,2]:="Poodle"
vArray[2,3]:="2"

vArray[3,1]:="Horse"
vArray[3,2]:="Arabian"
vArray[3,3]:="14"

Fornext(x; 1; 3)
	   MessageBox(; "Array Test";          
"The "+vArray[x,2]+" " + vArray[x,1]+" is "+          
vArray[x,3]+" years old.")
Endfor
          

As a bonus this month, I've included a downloadable macro that uses an
array. It is Version 1 of a foreign language Flashcard macro that
I created to help me learn Japanese. (I am putting the finishing
touches on a more robust version that will be available for a fee.)

You'll need a utility such as WinZip to unzip it, then open it in
WordPerfect. If the macro toolbar does not appear, click
Tools > Macro > Macro Bar, then click the Save & Compile
button to compile it. This macro was tested in WPWin 8, but
should work in WPWin 7 (Win95) without any adjustments. It may
need some minor adjustments in WPWin 6.1.

Go to the bottom of the macro to see where the array variables are set.
The first element is the Japanese word, and the second element is the
English word. The variables vLang1 and vLang2 in the macro represent
these two languages. These two variables are used throughout the macro
to specify whether the Japanese or English word is displayed in the upper
box on the dialog.

Once you have it compiled, run it. The first word will appear on the dialog.
Use the buttons to control your progress in the macro.

Click to download Flashcard.zip.

For more information on arrays, see the WordPerfect Macro Online Help,
or my book.

For information on ordering my book see my web page.

Copyright Notice: The information included in the PerfectScript Journal is protected by US Copyright. The author grants you the right to use the routines in your own macros as needed. You may not sell, distribute, or publish them in any form.
If you choose to use the information here, you do so entirely at your own risk. No representations are made regarding the fitness of this information for your   particular purpose, or for your ability or inability to use the information. You   are advised to make backups of all relevant files before implementing any suggestion  or technique.
© Copyright 1998 by J. Jeppson.