Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numeric addition from an array

Status
Not open for further replies.

logx

Technical User
Jul 24, 2006
14
RS
Hi,

I'm very new to ASP and have been trying to figure how to do this for a while now. I have an array taken from recordset using GetRows() and I wish to do a numeric addition of all the numbers in the array so that I get a total value.

eg. array elements are 1,2,3,4,5

total = 1+2+3+4+5

Thanks in advance!

 
Just convert the array values to numbers. Providing they are all integers, you could just use CInt(arr(1)), etc...

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
You could do a loop using LBound() and UBound()

That way you don't need to know how many elements are in your array at the time the code is written.

Something like this:
[tt]
TotalValue = 0
For x = LBound(TheArray) To UBound(TheArray)
TotalValue = TotalValue + TheArray(x)
Next

Response.Write TotalValue
[/tt]

... but remember that GetRows returns a two dimensional array.
 
sample code...
Code:
dim total
total = 0

for i=0 to UBOUND(myarray)
  total = total + CINT(array(i))
next
response.write total

-DNG
 
Woah... same answer posted at the same time!
 
Thanks Chopstik.

I'm having the problem with the following code:

resultsQ1=0
columnLoopA=0
myrecordset = rs(0)

myarray = rs.GetRows()

results=split(myarray,",")

WHILE columnLoopA < 6
resultsQ1 = resultsQ1 + results(columnLoopA)
columnLoopA = columnLoopA + 1
WEND

I keep getting:

Microsoft VBScript runtime error '800a000d'
Type mismatch

/output.asp, line 157

line 157 matches results=split(myarray,",")

I suppose the way I'm loading data from the db into the array isn't quite alright for the above method.
 
Split() is used to convert a string into an array.... so you pass in a string and get back an array.

...but the variable myarray is not a string it is already an array because GetRows() returns an array....



 
Thanks guys.

When I use:

myarray = rs.GetRows()

dim total
total = 0

for i=0 to UBOUND(myarray)
total = total + CINT(myarray(i))
next
response.write total

I get:

Subscript out of range: 'myarray'

I get the same problem when I use the code Sheco posted.
 
Either use
Code:
for i = 1 to UBOUND(myarray)
or
Code:
for i = 0 to UBOUND(myarray) - 1
.

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Code:
dim total
total = 0
For lnRowCounter = 0 To Ubound(MyArray,2) 
For lnColumnCounter = 0 To Ubound(MyArray,1)
total = total + CINT(MyArray(lnColumnCounter, lnRowCounter))
Next
Next
Response.Write total

-DNG
 
MyArray is a two dimensional array gfenerated from a GetRows call, per the OP's 3rd post.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top