 |
ASP Pro Code Clinic As of Oct 5, 2005, this forum is now locked. No posts have been deleted. Please use "Classic ASP Professional" at: http://p2p.wrox.com/forum.asp?FORUM_ID=56 for discussions similar to the old ASP Pro Code Clinic or one of the other many remaining ASP and ASP.NET forums here. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP Pro Code Clinic 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
|
|
|

June 25th, 2004, 08:32 AM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
Get Highest Array Value
I am looking at optimizing some code an would appreciate any advice. To get the highest value in an array:
goTil = ubound(finalArray)
For a = b To goTil
value = finalArray(count)
countTwo = 0
For c = d To UBound(finalArray)
if finalArray(countTwo) >= value then
highest = finalArray(count)
End if
countTwo = countTwo + 1
Next
count = count + 1
Next
My objective is to speed some pages up, thank you in advance.
Wind is your friend
Matt
__________________
Wind is your friend
Matt
|

June 25th, 2004, 08:41 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Are there going to be just numbers in that array?
_________________________
-Vijay G
 Strive for Perfection 
|

June 25th, 2004, 08:47 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Anyways, you can take a look at this and modify the way you want it.
Sorting Arrays (VBScript Implementation)
Cheers!
_________________________
-Vijay G
 Strive for Perfection 
|

June 25th, 2004, 08:52 AM
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi Matt,
As you are just looking for the highest value and not sorting, you could just do something like:
Code:
Dim myArray: myArray = Array(1, 5, 8, 2, 65, 23, 97, 4, 0, 6, 101)
Dim storeMax: storeMax = 0
Dim i
For i = 0 To UBound(myArray)
If myArray(i) > storeMax Then
storeMax = myArray(i)
End If
Next
Response.Write storeMax
HTH,
Chris
|

June 25th, 2004, 08:59 AM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
Hi There
Yes, They are stored as varchar's like 1/04 - 2/04 - 3/04 There are 1000's of them, this number is and will grow reasonably quickly (There is a good reason for these being varchars - they dont get inserted via a web page, it's a delphi executable)
My objective is to extract all numbers out for each originator and detect:
A..Is there any duplicates, if so list them.
B..Is there any missing in a sequential order, if so list them.
note: the sequential part is before the / after represents the current year
So
I strip the / and put them into an array. The above function is then used to detect the highest to display the next number expected (also a requirement in the report). Eg if highest is 1903 - this is the 19th submission for the year 03 therefore add a hundred = 2003
Wind is your friend
Matt
|

June 25th, 2004, 08:59 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Yes, this can help you.
Code:
Dim finalArray, HighVal
finalArray = split("10,21,52,49",",") ''' just to populate my own numbers
For i = lbound(finalArray) To ubound(finalArray)-1
HighVal=finalArray(i)
if cint(HighVal) < cint(finalArray(i+1)) then HighVal=finalArray(i+1)
Next
response.write HighVal
Cheers!
_________________________
-Vijay G
 Strive for Perfection 
|

June 25th, 2004, 09:05 AM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
Chris
I use getRows to populate the array and it is huge, I dont believe youir suggestion is an option:
;;im myArray: myArray = Array(1, 5, 8, 2, 65, 23, 97, 4, 0, 6, 101)
Each element in the array must be compared to every other element, nested for loops are neccessary.
Wind is your friend
Matt
|

June 25th, 2004, 09:08 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Matt,
That does scan through all the values and stores the highest of them all at any point of time. So you don't have to use nested loops.
I think Chris's and mine are almost the same logic.
Quote:
quote:myArray = Array(1, 5, 8, 2, 65, 23, 97, 4, 0, 6, 101)
|
I hope this was shown there just to use some sample values in the array, you can still populate your array with getrows as you do.
Any queries still?
_________________________
-Vijay G
 Strive for Perfection 
|

June 25th, 2004, 09:11 AM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
happygv
Nice, It seems 2 for loop wernt neccesary.
Thankyou for your time, have a nice day
Wind is your friend
Matt
|

June 25th, 2004, 09:17 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Matt,
As you said, you got 1000s of such values in the array, I have come up with another one, this is much faster than the one Chris/myself posted before.
Code:
Dim finalArray, HighVal
finalArray = split("10,21,52,49,200,32,45,45,100",",")
For i = lbound(finalArray) To ubound(finalArray)-1
HighVal=finalArray(i)
if cint(HighVal) < cint(finalArray(i+1)) then
HighVal=finalArray(i+1)
i=i+1
Response.write i & "<br>"
End If
Next
Response.write HighVal
This is because, it skips the subscript that is already checked and stored as HIGHVAL. You can have a response.write in the for loop to see the difference between the 2 codes posted so far.
The code in blue does the magic.;)
Cheers!
_________________________
-Vijay G
 Strive for Perfection 
|
|
 |