Handling Arrays in VBScript
An array is a collection of logically related data items, where each item can either be accessed in a sequential order, or through supplying its index position. Consider this: dim names(5) names(0)="Rohit" names(1)="Prakash" names(2)="Asha" names(3)="Tom" names(4)="Lata" names(5)="Kailash" for i=0 to 5 document.write names(i) & " " next Here we declare an index of dimension 5, which later on, comes to store six names, as, an index in an array starts at 0. Then the for loops dumps...
An array is a collection of logically related data items, where each item can either be accessed in a sequential order, or through supplying its index position.
Consider this:
Here we declare an index of dimension 5, which later on, comes to store six names, as, an index in an array starts at 0.
Then the for loops dumps the entire array onto the HTML page. People familiar with the for() {.} loop of JavaScript will notice that the for.next loop of VBScript is less cryptic.
Since now we have learnt how to create a VBScript array, let us try to sort this array. We'll sort the array in ascending order, that is, from the lowest to the top most in the alphabetical array.
So what's the logic?
1. Compare the first name with the second name 2. If the second name is lower than the first name, then swap 3. else 4. Compare the first name with the third name 5. If the third name is lower than the first name, then swap 6. else 7. Compare the first name with the fourth name 8. If the fourth name is lower than the first name, then swap 9. else 10. Compare the first name with the fifth name 11. If the fifth name is lower than the first name, then swap
So by the time we have reached the 11th step, we have the lowest name at the first position.
The second round starts at the second index index, as the first we already know is the lowest.
1. Compare the second name with the third name 2. If the third name is lower than the second name, then swap 3. else 4. Compare the second name with the fourth name
and so on
I hope you've understood the logic now. Let's see how it looks programmatically:
There is a nested loop here. Study the flow of the for i=0 to 4 loop. The outer loop holds the index of the item that will be compared with every other, next item. For instance, when the first cycle of the loop begins, i is zero, and when the program ends the nested loop, j starts at i+1, that is, 1. So turn by turn, item at index zero is compared with every item in the array. The outer array goes only till 4 because when the nested loop begins, the second last item will be compared with the last item: j=i+1
Save the file, and check it out in your browser.
Now we see the use of a dynamically defined array, and how that array can be filled using the inputbox() function.
strings is an array that is re-dimensioned whenever the user types in a new string. When we declare an array, we reserve the memory space for that array, that is, if we say
dim arr(20)
it means we are reserving the space for twenty data items. But what if the user enters just five items? The rest of the space is wasted. To avoid this, we declare a dynamic array.
In the above script, we have a loop whose continuance depends on the user input. It keeps on loop until the user enters "fin". So, when "fin" is not entered, only then the dimension of the array strings is increased.
When we are re-dimensioning an array, we have to use the keyword preserve to preserve the existing content of the array. In its absence, the array gets re-dimensioned, but the values are lost.
Exit do forces the program to come out of the loop if the condition that terminates it , occurs.
ABOUT THE AUTHOR
Amrit Hallan is a freelance web designer. For all web site development and web promotion needs, you can get in touch with him at amrit@bytesworth.com or http://www.bytesworth.com. For more such articles, visit http://www.bytesworth.com/articles You can subscribe to his newsletter [BYTESWORTH REACHOUT] on Web Designing Tips & Tricks by sending a blank email at bytesworth-subscribe@topica.com
|