Handling,Arrays,VBScript,array computer Handling Arrays in VBScript
----------------------------------------------------------Permission is granted for the below article to forward,reprint, distribute, use for ezine, newsletter, website,offer as free bonus or part of a product for sale as longas no changes a Gone are those times when the companies and the organisations didn't need a hi-tech system to handle them. Owing to the considerable increase in the business sector and thus, an enormous increase in the complexity of the organisational struc
An array is a collection of logically related data items, whereeach item can either be accessed in a sequential order, orthrough supplying its index position.Consider this:Here we declare an index of dimension 5, which later on, comes tostore 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 noticethat the for.next loop of VBScript is less cryptic.Since now we have learnt how to create a VBScript array, let ustry to sort this array. We'll sort the array in ascending order,that is, from the lowest to the top most in the alphabeticalarray.So what's the logic?1. Compare the first name with the second name2. If the second name is lower than the first name, then swap3. else4. Compare the first name with the third name5. If the third name is lower than the first name, then swap6. else7. Compare the first name with the fourth name8. If the fourth name is lower than the first name, then swap9. else10. Compare the first name with the fifth name11. If the fifth name is lower than the first name, then swapSo by the time we have reached the 11th step, we have the lowestname at the first position.The second round starts at the second index index, as the firstwe already know is the lowest.1. Compare the second name with the third name2. If the third name is lower than the second name, then swap3. else4. Compare the second name with the fourth nameand so onI hope you've understood the logic now. Let's see how it looksprogrammatically:There is a nested loop here. Study the flow of the for i=0 to 4loop. The outer loop holds the index of the item that will becompared with every other, next item. For instance, when thefirst cycle of the loop begins, i is zero, and when the programends the nested loop, j starts at i+1, that is, 1. So turn byturn, item at index zero is compared with every item in thearray. The outer array goes only till 4 because when the nestedloop begins, the second last item will be compared with the lastitem: j=i+1Save the file, and check it out in your browser.Now we see the use of a dynamically defined array, and how thatarray can be filled using the inputbox() function.strings is an array that is re-dimensioned whenever the usertypes in a new string. When we declare an array, we reserve thememory space for that array, that is, if we saydim arr(20)it means we are reserving the space for twenty data items. Butwhat if the user enters just five items? The rest of the space iswasted. To avoid this, we declare a dynamic array.In the above script, we have a loop whose continuance depends onthe user input. It keeps on loop until the user enters "fin". So,when "fin" is not entered, only then the dimension of the arraystrings is increased.When we are re-dimensioning an array, we have to use the keywordpreserve to preserve the existing content of the array. In itsabsence, the array gets re-dimensioned, but the values are lost.Exit do forces the program to come out of the loop if thecondition that terminates it, occurs. Article Tags: First Name Then, First Name, Second Name, Name Then
Handling,Arrays,VBScript,array