SitePoint Sponsor

Thread: looping variable

1. looping variable

I have a quick question. I have a variable that goes through a loop, and each time it appends the new value to the end using a comma. Example:

1st time through:
a = 1
2nd time through:
a = 1, 4
3rd time through:
a = 1, 4, 2

So now after my loop is all done I have this value where each value is seperated by a comma. What I want to do is compare each of the values and display only the largest number. Any ideas on how to do this? Thanks

2. Not quite sure where the source of the numbers is. A recordset? An array?

Anyway, do you mean something like this?

Code:
```Dim LargestValue    :  LargestValue = 0

' Assuming a recordset...
Do while not rs.eof
HoldValue = clng(rs("returnedvalue"))
if HoldValue > LargestValue then
LargestValue = HoldValue
end if
OutputString = OutputString & "," & HoldValue
rs.MoveNext
Loop```
At the end, LargestValue would hold the largest value entered into the string...

3. Or a variation on this Wrox exercise.

Code:
```<%
Dim strText, intSpace, strWord, intCounter
intSpace = 1
strText ="the quick brown fox jumped over the lazy dog"
For intCounter = 1 To len(strText)+1
If mid(strText,intCounter,1) = " " Or intCounter = len(strText)+1 Then
strWord = mid(strText,intSpace,intCounter-intSpace)
intSpace = intCounter
strWord = trim(strWord)
%>```

4. This is just pulling out the three values again. I need to show just the largest number of the value, without commas.

5. Well once you've pulled out the values it's a doddle to just do an If or a function to find the highest one ... check the value -- if it's the highest so far, keep it, if it's not, overwrite it with the current one .......

6. nevermind I figured it out, I was displaying the wrong variable

7. Let me throw this one out there. I got the largest number, now I'm trying for smallest with no luck...code:

HoldValue2 = averagetime
if HoldValue2 < LargestValue2 then
LargestValue2 = HoldValue2
holduser1 = user1
end if

Problem is since largestvalue2 starts off equalling 0 then no number is ever going to be lower than 0. I tried starting largestvalue2 at a large number with no luck. Any ideas?

8. Are your values ever 0?

9. No, the only value that starts off zero is the largestvalue2, but since it starts off as 0 then nothing will ever be smaller.

10. The why not do something like this in your if statement: If HoldValue < SmallestValue OR SmallestValue = 0 then

Posting Permissions

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