Total an array?

1. Senior Member
Join Date
Dec 1969
Posts
133

## Total an array?

Trying to get the total amount from an array of values..<BR>&#039;split the strings from the form<BR>items=Split(Request.Form("Item"), ", ")<BR>costs=Split(Request.Form("Cost"), ", ")<BR>qtys=Split(Request.Form("Qty"), ", ") etc etc<BR>&#060;%<BR>For I=0 To UBound(items)<BR> If IsNumeric(qtys(I)) And Len(qtys(I))&#062;0 Then<BR>%&#062; <BR>html stuff here<BR>&#060;p align="center"&#062; &#060;input type="text" readonly name="Item" value=&#060;%=(items(I))%&#062; &#060;/td&#062;<BR>&#060;p align="center"&#062; &#060;input type="text" readonly name="SubTtl" value=&#060;%=CInt(qtys(I))*CDbl(costs(I))%&#062;& #060;/td&#062;<BR>&#060;%<BR>End If<BR>Next<BR>%&#062;<BR>Any ideas on how can I total the subtotals from this line?<BR>&#060;p align="center"&#062; &#060;input type="text" readonly name="SubTtl" value=&#060;%=CInt(qtys(I))*CDbl(costs(I))%&#062;& #060;/td&#062;<BR><BR>Whatever I&#039;ve tried I keep getting subscript out of range, or only the subtotal from the first or the last line<BR><BR>Thanks<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Do the math BEFORE html

&nbsp;<BR>total = 0.0<BR>For I=0 To UBound(items)<BR> If IsNumeric(qtys(I)) And Len(qtys(I))&#062;0 Then<BR> item = items(I)<BR> subtotal = CInt(qtys(I))*CDbl(costs(I))<BR> total = total + subtotal<BR>%&#062; <BR> ...<BR> &#060;input type="text" readonly name="Item" value="&#060;%=item%&#062;" &#062;<BR> &#060;input type="text" readonly name="SubTtl" value="&#060;%=FormatCurrency(subtotal)%&#062;<BR> ...<BR>&#060;%<BR> End If<BR>Next<BR>%&#062;<BR>... output total here ...<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## Sorry...missing characters...

&#060;input type="text" readonly name="SubTtl" value="&#060;%=FormatCurrency(subtotal)%&#062;" &#062;

4. Senior Member
Join Date
Dec 1969
Posts
133

## RE: Do the math BEFORE html - I WAS SO CLOSE

I was just about there...THANKS<BR>I was trying to do it this way...<BR>For I=0 To UBound(items)<BR>If IsNumeric(qtys(I)) And Len(qtys(I))&#062;0 Then<BR>subTotal= CInt(qtys(I))*CDbl(costs(I))<BR>%&#062; <BR>...<BR>&#060;input type="text" readonly name="Item" value="&#060;%=item%&#062;" &#062;<BR>&#060;input type="text" readonly name="SubTtl" value="&#060;%=(subtotal)%&#062;<BR>...<BR>&#060;% <BR>Total = 0<BR>Total = Total +subTotal <BR>End If<BR>Next<BR>%&#062;<BR>&#060;td width="9%"&#062;&#060;%=(Total)%&#062;&#060;/td&#062;<BR><BR>THANKS<BR><BR><BR> <BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Indeed...now do you see...

...what you were doing wrong?<BR><BR>Only ONE LINE was wrong, really. And it was only in the wrong place.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
133

## RE: Indeed...now do you see...

I&#039;m not sure... this is only my 2nd experiment with arrays ;)<BR>I&#039;m guessing that by doing the math right before the End IF and Next, the math was just looking at the last element of the array and figuring the total on that?<BR>By doing it B4 the html, it takes all the subtotals, adds them, then loops thru with the Next statment and now that we&#039;ve gone thru the entire array we can write a total that encompasses ALL of the array...<BR>Am I on the right track here?<BR>Thanks again <BR>

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## No, much simpler than that...

It wasn&#039;t really important whether you do the math before or after the HTML. I only do it before so I don&#039;t have to do the multiplication twice: I can use the calculated subtotal in the display for that line. But two multiplies in an ASP page aren&#039;t exactly going to make and measurable difference in performance.<BR><BR>No, your only mistake was that you were doing<BR> Total = 0<BR>*INSIDE* the loop!<BR><BR>So EACH TIME throught the loop, you were WIPING OUT whatever you had put into the Total value on the prior time through the loop!<BR><BR>No wonder, then, you ended up with Total equal to the subtotal of the last time through.<BR><BR>You just needed to move<BR> Total = 0<BR>to *before* the loop, thus initializing it only *once*.<BR><BR>

8. Senior Member
Join Date
Dec 1969
Posts
133

## RE: No, much simpler than that...OK what about thi

That makes sense...<BR>It also probably explains why I was having trouble using an ElseIf or another If statement to display something if there was no quantity filled out or if the quantity wasn&#039;t numeric.<BR>What I came up with is to put this after the <BR>End If<BR>Next<BR><BR>&#060;td width="91%" colspan="5"&#062; &#060;%If subTotal ="" Then <BR>response.write "Either you didn&#039;t enter a quantity, or the quantity isn&#039;t numeric."<BR>End If<BR>%&#062;<BR><BR>Is this the proper way to go about it, or is there a better way?<BR>Thanks<BR>

9. Senior Member
Join Date
Dec 1969
Posts
96,118

## Two things wrong with that...

(1) You don&#039;t initialize subTotal to any particular value each time through the loop, so if you don&#039;t change it in this time through the loop it will have the same value it had last time through!<BR><BR>(2) subTotal is a *NUMBER*, so why would you compare it to a string?<BR><BR>But, actually, you *could* fix both of those with one operation:<BR><BR>&#060;%<BR>For ...<BR> subTotal = "" &#039; why not?<BR> IF ...<BR> subTotal = qty * price<BR> ...<BR> End If<BR> ...<BR><BR> If subTotal = "" Then<BR> Response.Write ...<BR> End If<BR> ...<BR>Next<BR>%&#062;<BR><BR>But I don&#039;t understand why you need to do that? Why isn&#039;t that just an ELSE on the IsNumeric of the quantity?<BR><BR>**********<BR><BR>total = 0.0 <BR>For I=0 To UBound(items) <BR> If IsNumeric(qtys(I)) And Len(qtys(I))&#062;0 Then <BR> item = items(I) <BR> subtotal = CInt(qtys(I))*CDbl(costs(I)) <BR> total = total + subtotal <BR>%&#062; <BR> ... <BR> &#060;input type="text" readonly name="Item" value="&#060;%=item%&#062;" &#062; <BR> &#060;input type="text" readonly name="SubTtl" value="&#060;%=FormatCurrency(subtotal)%&#062; <BR> ... <BR>&#060;% <BR> Else &#039; we did not put out the item or subtotal<BR> response.write "Either you didn&#039;t enter a quantity, or the quantity isn&#039;t numeric."<BR> End If <BR>Next <BR>%&#062; <BR>

10. Senior Member
Join Date
Dec 1969
Posts
133

## RE: Two things wrong with that...

I did it that way because no matter how I attempted to write the statement that did value checking, it either didn&#039;t work properly or it repeated the response.write statement. I had already tried your Else statement right B4 the End If, but it duplicates the response.write (because it&#039;s in the loop right?)<BR>I&#039;d tried to put an IF or an Else up in the first part of the code and compare the "string" but didn&#039;t get the result I was expecting with that idea either.... so this was the only way I could think of to get it done...<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•