Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 25th, 2005, 03:41 AM
Authorized User
 
Join Date: Jul 2003
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I add up values in an array?

Hi there

I was hoping someone might be able to help me with the following problem. I have a string i'm passing through from one page to another, in a variable called totalvalue, with each value separated by a comma. The string could contain any number of values, all being whole numbers, all greater or equal to 0.

example 1: 4,6,7
example 2: 1,0
example 3: 4
example 4: 5,7,2,8,0,4
etc

I somehow need to add these values up. So, in my examples above, i would like to get the following results

example 1: 17
example 2: 1
example 3: 4
example 4: 26

I think I need to do:

Dim totalvalue
totalvalue = request(totalvalue)

Dim arytotalvalue
arytotalvalue = split(totalvalue,",")

...to split the values up (would this work in example 3 where there isn't a comma?), but don't know how to do the adding bit when there could be any number of values in totalvalue.

Can anyone help?

Thanks in advance

Lucy xx
 
Old May 25th, 2005, 04:15 AM
Authorized User
 
Join Date: Jul 2004
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, split would work even in case of one element in the array.

Code for adding:
        For i = 0 To arytotalvalue.Length - 1
            sum = sum + arytotalvalue(i)
        Next

Spacy

 
Old May 26th, 2005, 05:45 AM
Authorized User
 
Join Date: Jul 2003
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for getting back to me.

Unfortunately this seems to give me an error message:

Microsoft VBScript runtime error '800a01a8'

Object required

/return.asp, line 55


which relates to the For i=0 line

I've just noticed that my values are actually coming through as this: 4, 6, 7 and not 4,6,7 (i.e. there is a space after each comma)

Don't know whether that makes a difference!

Can anyone help?

Cheers

Lucy

 
Old May 26th, 2005, 05:51 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Lucy,

Try...
Code:
For i = 0 To UBound(arytotalvalue) - 1
    sum = sum + arytotalvalue(i)
Next
HTH,

Chris

 
Old May 27th, 2005, 04:34 AM
Authorized User
 
Join Date: Jul 2003
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Chris

Still no joy i'm afraid! :(

I've added the line...

response.write sum

after 'next' to see what's happening...

If I use the example
3, 4
and use your code, I get 3 showing on my page. If I take the -1 bit out of your code (what does that do?) then I see
3 4
but nothing's adding cos i should be seeing 7!

Any ideas....anyone?

Thanks in advance

Lucy
 
Old May 27th, 2005, 05:11 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Lucy,

I think the probelm may be that you haven't given a value to "sum" before you start adding to it & it's getting treated like a string.
You don't need the -1 (think you'd worked that out).
Try this...
Code:
Dim i, sum: sum = 0
For i = 0 To UBound(arytotalvalue)
    sum = sum + CLng(arytotalvalue(i))
Next
Response.Write sum
Cheers,

Chris

 
Old May 31st, 2005, 05:26 AM
Authorized User
 
Join Date: Jul 2003
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Chris

A big big thank you for all your help - this now works perfectly!

Sorry if any of my questions, or lack of coding knowledge, were a bit basic - I haven't really used arrays and for loops before so wasn't entirely sure about what to declare/set to zero etc.

You're a star :)

Cheers

Lucy





Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding the values within an array Dave_Richards Beginning PHP 0 November 8th, 2006 04:34 PM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM
Add elements to array TSEROOGY Javascript 5 February 22nd, 2004 03:16 PM
change order of an array automatically after add SauSaigon Beginning PHP 7 February 22nd, 2004 12:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.