Important alert: (current site time 10/20/2014 9:49:35 PM EDT)
 

VB icon

count items in an array

Email
Submitted on: 8/30/2005 2:29:45 PM
By: Nate T Norris  
Level: Beginner
User Rating: By 1 Users
Compatibility: ASP (Active Server Pages), HTML, VbScript (browser/client side)
Views: 18929
author picture
(About the author)
 
     Used to count Items in an array! if you know of a better/different way to do this, add you comment below.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :count items in an array
'**************************************
Copywrite 2004 Nate T Norris - All Rights Reserved. You may use this code freely as Long as the header lines remain intact.
11/9/04 - http://www.planetwebdev.com
CodeWolfe@flashai.com
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: count items in an array
' Description:Used to count Items in an array! if you know of a better/different way to do this, add you comment below.
' By: Nate T Norris 
'
' Inputs:An Array of at least one item
'
' Returns:The total number of items in interger form
'
' Assumes:Self Explanatory!
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=9033&lngWId=4'for details.'**************************************

'******************************************
'Count items in an Array
'Copywrite 2004 Nate T Norris - All Rights Reserved. 
'You may use this code freely as Long as the header lines remain intact.
'11/9/04 - http://www.planetwebdev.com
'codewolfe@flashai.com
'******************************************
dim x
x=0
for each item in myarray
	x = x + 1
next
response.write "<br>Num in array: " & x


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

11/20/2004 12:34:49 AMGurgen Alaverdian

Here's one-liner: x = UBound(myarray) + 1
(If this comment was disrespectful, please report it.)

 
11/20/2004 11:28:42 AMStilgar

C'mon, thats lame. Use UBound as suggested above - it's one line of code, and saves system resourcrs. For two dimensional (or above) arrays, you can use UBound on the other dimensions too and multiply the results.

Stilgar.
(If this comment was disrespectful, please report it.)

 
11/22/2004 11:59:02 AMNathan T Norris

Now that's what I call a discussion. Thanks guys. BTW Stilgar, speaking of lame, you misspelled resources. - Great suggestion Gurgen Alaverdian.
(If this comment was disrespectful, please report it.)

 
12/2/2004 9:20:25 AMDJenk47

You'll really need to be careful using just UBound(array)+1. You are assuming the array is 1-based. A better method is UBound(array)-LBound(array)+1.
(If this comment was disrespectful, please report it.)

 
7/1/2005 9:55:21 AM

In VBScript and ASP, the LBound of an array is always 0, so UBound is not an issue. Even if you begin by initializing an array with, say, element 1, UBound will always be 0. I believe that VB and other programming languages do have functions/methods of defining arrays to begin with an index other than zero.

(If this comment was disrespectful, please report it.)

 
7/1/2005 9:59:50 AM

By the way, Stilgar. Remember, we were all beginners at one time or another, and we have all used programming code that was less than perfect. These forums are a learning tool, and as such, I believe "insults" or negative comments are unwarrented. It is more instructive, and the point may be made better, by stating why the code is not efficient, and then offer a better way.
Just my 2-cents worth.
Chalmers

(If this comment was disrespectful, please report it.)

 
7/6/2005 10:51:41 PMKilroy

For those of you who are unaware there is also a way to determine the size of an array when it has more than one dimension [i.e. myarray(10,20)]. Granted typically you would have created this array so there would be no need to determine this. However, for those of you who know that a record set can be dumped into a variable using the GetRows comand you will know that you have no idea what the dimensions are. To find out what the size is you would use the UBound(myArray,1) and UBound(myArray,2) etc... For each diemsion of the array. After all, arrays are not always square with the same number of rows and columns.

Happy coding!!!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.