Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBS: Counting number of items in an Array

How could I investigate simply the number of items in a given array. I used this code, but it's a bit laborious

myArr=Array("frog", "cat", "bat", "rat", "horse")
 for i=0 to UBound(myArr)
 ' Msgbox i +1 & ".item: " & myArr(i) 
 next
Msgbox i & " items in this array:" & vbcrlf & Join(myArr)

Thanks

like image 460
Gurkenhobel Avatar asked Mar 18 '23 20:03

Gurkenhobel


2 Answers

Umm... you have it in your code. UBound(Array) returns the upper bound, which is the highest existing item. UBound(myArr) + 1 is its length, because the index is zero based.

like image 107
JaGoTu Avatar answered Mar 28 '23 08:03

JaGoTu


Msgbox (ubound(myArr) + 1) & " items in this array:" & vbcrlf & Join(myArr)

EDIT: While you are using ubound already, why do you need a loop & a variable i to count.

like image 41
shahkalpesh Avatar answered Mar 28 '23 07:03

shahkalpesh