
Arrays Introduction: What it is and what it can do for you
Array is used to store different data collection in a single variable. It is commonly used to store the same type of data. E.g. You want to store 4 scores of different players. You will need to have 4 different variables for each player, like this:
Score1 = 0
Score2 = 0
Score3 = 0
Score4 = 0
If you have 12 players, then you will need12 variables. When you change the score of 12 players, you will have 12 lines of something like this: Score1 = Score1 + 1 .Well, But when you add the score, you have to check who wins. You will be typing something like this:
In this example, WinPlayer stores the no. of player that wins
Do case
case WinPlayer = 1
Score1 = score1 + 1
case WinPlayer = 2
Score2 = Score2 + 1
Etc. So on, and so on….
If you want to print all 12 scores, you will have to type 12 lines of something like:
@ 10,10 say “Player 1 score: “ + Score1
Nobody would want to do this every time to want to change the score. It takes to long to type and too much coding. Array can solve this problem for you.
Creating Arrays
Use Declare command
Declare [Variable’s name] ( [No. of dimensions/ size of array] )
In an above example, it needs to store 12 different scores for each player, so it would be:
Declare Score(12) 12 means you will store 12 set of data
Using Arrays
Arrays can be used just like normal variables with the exception that the index of an array has to be specified inside the parentheses. E.g.
- Score(2) = 5 Change Player 2's score to 5
- Score(4) = Score(4) + 1 Add one more point to Player 4's score
Some of you may think that this does not help a bit, because in order to determine who to add a point to when wins, we still have to do the following:
Do case
case WinPlayer = 1
Score1 = score1 + 1
case...
How ever, there is a way to do those long lines of case statements into a single line! Here is the magic:
Score(WinPlayer) = Score(WinPlayer) + 1
Explanation: If WinPlayer is 2 then, Score(2) = Score(2) + 1. This automatically add 1 score to any players that wins. If you want to print out the scores of all players, you can just create a loop to help you just like this:
CurrentNo = 1
Yposition = 10
Do while CurrentNo <= 12
@ YPosition, 10 say “Player “ + alltrim(Str(Score(CurrentNo))) + “’s score: “ + alltrim(str(Score(CurrentNo)))
Yposition = Yposition + 1
CurrentNo = CurrentNo + 1
Enddo
Multi-dimensional Arrays
Above example only uses one dimensional array. We can take the above example one step further. What if we now want to store multiple scores for each player? Let's say that there are 12 players and I need to store 5 scores from 5 different games for each player. Here is what we can do is to:
Declare Score(12,5)
This actually creates 2 dimension array. There are 12x5 = 60 locations to store the data. For 2 dimensional-array, it is easier to visualize it as table like the one below:
| |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
| 1 |
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
Let's say I want to set the score of player 6, game 2 (box highlighted in yellow) to 8, I would do:
Score(6,2) = 8
You are not limited to one or just two dimensional arrays, you can have 3,4,5,10,.... whatever how many dimensions you need. However, it is harder to visualize many dimensional arrays, but the concept is the same.
For more information, consult Vfp worksheet 6 or helpfile.
Coming soon: Using Arrays with databases. E.g. How to obtain record data into arrays, etc.