determine items in array

J

JT

how do i determine how many items are in an array? the following code
creates an array of values each time a space is found in a name field. the
problem is that sometimes names have middle initials and sometimes they do
not. so i want to determine how many items exist in this array created by
the split BEFORE assigning values:

name_of_customer = Split(customer_name," ", -1, 1)

first_name = name_of_customer(0)
middle_initial = name_of_customer(1)

thanks much

jt
 
R

Ray at

Arrays in VBScript are zero based and cannot be changed to base 1 (option
base 1) as they can in VB. So, knowing that and knowing the UBound
function, the number of items in an array is the ubound of the dimension +
1.

name_of_customer = Split(customer_name," ", -1, 1)
NumberOfItems = UBound(name_of_customer, 1) + 1

The ",1" is an optional argument for single dimensional arrays. The array
dimensions start at 1, not 0.

Ray at work
 
R

Ray at

p.s. A more universal way that would make it so you don't have to worry
about 1 or 0 based arrays would probably be to use the lbound and the ubound
as so:

iNumberOfItems = UBound(theArray, theDimension) - LBound(theArray,
theDimension) + 1

Ray at work
 
B

Bob Barrows

JT said:
how do i determine how many items are in an array? the following code
creates an array of values each time a space is found in a name
field. the problem is that sometimes names have middle initials and
sometimes they do not. so i want to determine how many items exist
in this array created by the split BEFORE assigning values:

name_of_customer = Split(customer_name," ", -1, 1)

first_name = name_of_customer(0)
middle_initial = name_of_customer(1)

thanks much

jt
Use the Ubound function to determine the largest available index number for
the array. Since arrays are zero-based, add one to get the count of the
array's items:

elements = ubound(name_of_customer) + 1

HTH,
Bob Barrows
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top