UBound returns different result when array is created using Split() 
Author Message
 UBound returns different result when array is created using Split()

I have a question I hope someone might know the answer to.  I'm using
VBScript in my web applications and I notice a difference in behavior
when using the UBound() function with arrays.  If I create an array
like so:

Dim arrTest(4)

and then set the values...

arrTest(0) = "test 1"
arrTest(1) = "test 2"
arrTest(2) = "test 3"
arrTest(3) = "test 4"

... then use the Ubound of the array for a loop, I would use it like
this:

For i = 0 To UBound(arrTest)-1
...
Next

It would loop through all 4 of the items in the array. BUT, I found
that if I create an array using the Split() function, it works
differently:

strList = "test 1|test 2|test 3|test 4"

arrTest = Split(strList, "|")

For i = 0 To UBound(arrTest)
...
Next

... For some reason, when I use the Split function to create the array,
the UBound(arrTest) value will be 3, otherwise it's 4 if I create it
the previous way.

Do you know if this is a known behavior?  I couldn't find any
documentation on MSDN that talked about it.

Thanks!

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Fri, 11 Oct 2002 03:00:00 GMT  
 UBound returns different result when array is created using Split()
this "anomoly" is by design. when you dim arrTest(4) you are actually
creating FIVE cells in the array 0, 1, 2, 3, 4. (because VBS arrays start at
0) when you use the split routine you are only creating FOUR cells... 0, 1,
2, 3. the right way to do it is 0 to ubound(arrTest) and change your dim
line to dim arrTest(3)

--

Jim Bolla, Web/Software Developer
------------------------------------------------------
Distributed Network Software, LLC
HTTP://WWW.DNSLLC.COM
105 Poplar Street Erie, PA 16507
P:(814) 874-3344 | F:(814) 874-3343

Quote:

> I have a question I hope someone might know the answer to.  I'm using
> VBScript in my web applications and I notice a difference in behavior
> when using the UBound() function with arrays.  If I create an array
> like so:

> Dim arrTest(4)

> and then set the values...

> arrTest(0) = "test 1"
> arrTest(1) = "test 2"
> arrTest(2) = "test 3"
> arrTest(3) = "test 4"

> ... then use the Ubound of the array for a loop, I would use it like
> this:

> For i = 0 To UBound(arrTest)-1
> ...
> Next

> It would loop through all 4 of the items in the array. BUT, I found
> that if I create an array using the Split() function, it works
> differently:

> strList = "test 1|test 2|test 3|test 4"

> arrTest = Split(strList, "|")

> For i = 0 To UBound(arrTest)
> ...
> Next

> ... For some reason, when I use the Split function to create the array,
> the UBound(arrTest) value will be 3, otherwise it's 4 if I create it
> the previous way.

> Do you know if this is a known behavior?  I couldn't find any
> documentation on MSDN that talked about it.

> Thanks!

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Fri, 11 Oct 2002 03:00:00 GMT  
 UBound returns different result when array is created using Split()
Dim arrTest(4) is creating 5 elements not the 4 that you expect
Zero Based arrays : }
So split is really creating the correct number of elements
Change  Dim arrTest(4) to Dim arrTest(3)
put into your code
MsgBox join(arrTest,"|")
and you will see that all 4 elements are still in your array.

Cheers
--
David Hamel

NBT Inc.

Quote:

> I have a question I hope someone might know the answer to.  I'm using
> VBScript in my web applications and I notice a difference in behavior
> when using the UBound() function with arrays.  If I create an array
> like so:

> Dim arrTest(4)

> and then set the values...

> arrTest(0) = "test 1"
> arrTest(1) = "test 2"
> arrTest(2) = "test 3"
> arrTest(3) = "test 4"

> ... then use the Ubound of the array for a loop, I would use it like
> this:

> For i = 0 To UBound(arrTest)-1
> ...
> Next

> It would loop through all 4 of the items in the array. BUT, I found
> that if I create an array using the Split() function, it works
> differently:

> strList = "test 1|test 2|test 3|test 4"

> arrTest = Split(strList, "|")

> For i = 0 To UBound(arrTest)
> ...
> Next

> ... For some reason, when I use the Split function to create the array,
> the UBound(arrTest) value will be 3, otherwise it's 4 if I create it
> the previous way.

> Do you know if this is a known behavior?  I couldn't find any
> documentation on MSDN that talked about it.

> Thanks!

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Fri, 11 Oct 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Returning Array Created by SPLIT

2. Split, UBound, and a Dynamic Array

3. Problem assigning Split() result to an array

4. problem using component with asp page -- result of function not returned, instead original value returned

5. Quereies Return Different Results ADO vice Access

6. HELP...Queries return different results in VB than Access

7. Two identical queries returning different results

8. DAO/ADO return different results

9. DAO/ADO return different results

10. CreateRectRgn returns different results?

11. Split ->Unexpected Ubound

12. Help: Different result in distributed rpt using Crystal Reports6 with ActiveX)

 

 
Powered by phpBB® Forum Software