Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Determining Array length

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,39623
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Determining Array length
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Determining Array length - 11/11/2006 10:13:13 PM   
  TNO


Posts: 1399
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: online
Unlike JScript, there is apparently no direct method of obtaining the length of an Array. So my question is, has anyone come up with a creative way of determining the length of an array in VBScript?

This is what I could come up with:


      

Is there a better way?

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch
 
 
Post #: 1
 
 RE: Determining Array length - 11/12/2006 1:42:45 AM   
  ehvbs

 

Posts: 2222
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi TNO,

use UBound() in VBScript where you would use .length in Javascript, but remember,
that UBound() returns the last index:

  Dim a1D : a1D = Array( 10, 20, 30, 40, 50, 60, 70, 80, 90 )
  Dim a2D( 1, 2 )
  a2D( 0, 0 ) = "00"
  a2D( 0, 1 ) = "01"
  a2D( 0, 2 ) = "02"
  a2D( 1, 0 ) = "10"
  a2D( 1, 1 ) = "11"
  a2D( 1, 2 ) = "12"
  WScript.Echo "LBound( a1D    ) =", LBound( a1D ), "UBound( a1D ) =", UBound( a1D )
  WScript.Echo "UBound( a2D    ) =", UBound( a2D )
  WScript.Echo "UBound( a2D, 1 ) =", UBound( a2D, 1 )
  WScript.Echo "UBound( a2D, 2 ) =", UBound( a2D, 2 )

(in reply to TNO)
 
 
Post #: 2
 
 RE: Determining Array length - 11/12/2006 2:13:13 AM   
  TNO


Posts: 1399
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: online
Thanks for the reply. I considered this method but is not quite as foolproof as I'd like, for example:


      

This doesn't account for undefined variables and alerts 10 instead of 1.There  is no doubt a workaround using ReDim for this but I was hoping for something simpler

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ehvbs)
 
 
Post #: 3
 
 RE: Determining Array length - 11/12/2006 2:20:17 AM   
  TNO


Posts: 1399
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: online
Belated thought:
I'm apparently thinking of VBScript arrays in a way non-related to this particular language:

Does “size” mean number of elements, bytelength of contents, memory consumed by the object?


Since Arrays in vbscript are a Function with a vice an object its no wonder I'm not getting what I expected.

Array(1,2,3,4,5,6,7)

Is the same thing as

Function(a,b,c,d,e,f,g,h)
   For each of the arguments, creat and return blah
End Function

So I guess the issue is semantics more than anything. The Ubound suggestions is probably best


_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to TNO)
 
 
Post #: 4
 
 RE: Determining Array length - 11/12/2006 10:01:03 PM   
  ginolard


Posts: 1082
Score: 21
Joined: 8/10/2005
Status: offline
Yeah it's a pain.  I sometimes use this function to accurately determine the length of array


      

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to TNO)
 
 
Post #: 5
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Determining Array length Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts