Wrox Programmer Forums
|
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
 
Old June 25th, 2004, 08:32 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default 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
 
Old June 25th, 2004, 08:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Are there going to be just numbers in that array?

_________________________
-Vijay G
Strive for Perfection
 
Old June 25th, 2004, 08:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old June 25th, 2004, 08:52 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

 
Old June 25th, 2004, 08:59 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

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
 
Old June 25th, 2004, 08:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old June 25th, 2004, 09:05 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

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
 
Old June 25th, 2004, 09:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old June 25th, 2004, 09:11 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

happygv

Nice, It seems 2 for loop wernt neccesary.

Thankyou for your time, have a nice day


Wind is your friend
Matt
 
Old June 25th, 2004, 09:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting highest count stolte XSLT 2 November 19th, 2008 04:00 PM
finding highest value Aprile Access VBA 3 October 3rd, 2007 07:13 AM
Finding the highest amount keithd Excel VBA 2 May 25th, 2005 01:44 PM
How to open only the record with highest ID ?! SKE Classic ASP Databases 2 May 16th, 2005 06:05 AM
Finding the highest value of a query ry Oracle 1 December 18th, 2004 12:40 PM





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