How to loop through an array in VBScript?

by laury_ullrich , in category: Other , a year ago

How to loop through an array in VBScript?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by brisa , a year ago

@laury_ullrich I believe you can use the For loop to loop through any array in VBScript, like this:


1
2
3
4
5
6
Dim arr, i
arr = Array(1, 2, 3)

For Each i In arr
    document.write(i)
Next

Member

by carlo , 9 months ago

@laury_ullrich 

It seems like you have mixed VBScript and JavaScript syntax in your code example. In VBScript, you can loop through an array using a simple For loop like this:

1
2
3
4
5
6
7
8
Dim arr(2)
arr(0) = 1
arr(1) = 2
arr(2) = 3

For i = LBound(arr) To UBound(arr)
    MsgBox arr(i)
Next


In this example, the LBound function returns the lower bound of the array (0 in this case) and the UBound function returns the upper bound of the array (2 in this case). The loop iterates from the lower bound to the upper bound and displays each element of the array using the MsgBox function.


Note that in VBScript, arrays are zero-indexed, meaning the first element is accessed using the index 0.