Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP 3 Classic ASP Active Server Pages 3.0 > Classic ASP Professional
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Classic ASP Professional For advanced coder questions in ASP 3. 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 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 16th, 2006, 09:44 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default VBScript find missing number function??

Is there a VBScript function to find missing numbers. Eg if I had a string:

1,2,3,4,5,6,7,8,9,14,10,11,12,13,15,16,17,21

The result would return 18,19,20

I am sure I have used it before however can ot find it in my code library, any vbScript .chm files and other documentation I have.

TYIA

Wind is your friend
Matt
__________________
Wind is your friend
Matt
Reply With Quote
  #2 (permalink)  
Old May 24th, 2006, 11:22 PM
Friend of Wrox
 
Join Date: May 2006
Location: San Diego, CA, USA.
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have never heard of an intrinsic vbscript function to do this.


Woody Z http://www.learntoprogramnow.com
Reply With Quote
  #3 (permalink)  
Old May 24th, 2006, 11:47 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Yes you are correct , there is not one. Sorry I should have posted the solution for others:

Function findMissing(str, delim)
    findMissing = ""
    ar = Split(str, delim)
    ar = sortIt(ar)
    For i = 0 To UBound(ar) - 1
        If CLng(ar(i)) + 1 <> CLng(ar(i + 1)) Then
            '' we found missing number(s)
            For j = 1 To (CLng(ar(i + 1)) - CLng(ar(i))) - 1
                findMissing = findMissing & CStr(CLng(ar(i)) + j) & ","
            Next
        End If
    Next
End Function

Function sortIt(ar)
    For j = 0 To UBound(ar)
        For i = 0 To UBound(ar) - 1
            If CLng(ar(i)) > CLng(ar(i + 1)) Then
                tmp = ar(i)
                ar(i) = ar(i + 1)
                ar(i + 1) = tmp
                Exit For
            End If
        Next
    Next
    sortIt = ar
End Function

Wind is your friend
Matt
Reply With Quote
  #4 (permalink)  
Old February 12th, 2007, 02:13 AM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

The function(s) above have been working well up until the end of last year. This week its come to my attention that:

> If the string is reasonably sequential it does what its supposed to do - find the missing numbers.
> If not it fails in a way which I am having trouble understanding. Below is an example of a string which fails:

1,2,3,4,5,6,7,8,10,11,12,13,16,17,18,19,20,21,22,2 5,26,27,28,29,31,32,33,35,37,38,36,30,9,39,40,42,4 3,44,45,24

The missing numbers in this string are 14,15,23,34 and 41. If I manually order the string sequentialy it works fine however if passed to the function in the order above the result of missing numbers is:

14,15,23,24,34,25,26,27,28,29,30,31,32,33,34,35,36 ,37,41

I have been looking at it for to long today. I can not work out why it says 14 is the first missing number when sequentialy 9 is. Would anybody have the time to look over it and advise me on the following:

> Are my functions a bit rough?
> If not can anyone see a mistake I have made or point out the logic why it fails in the manor it does?
> Would creating a function that orders the string sequentialy before I pass it into the above functions be prudent?

This one has be backed in a corner TYIA for any assistance

Wind is your friend
Matt
Reply With Quote
  #5 (permalink)  
Old February 12th, 2007, 04:44 AM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

I have now discovered if i remove the last integer value (24) from the comer delimited string above it works as it should. mmmm, very strange. Is my function(s) a bit tired and therefore lack scalability? The problem doesnt seem to be because the string is not ordered sequentialy however if they are placed this way it works, mmmmm...

To see this, if one has the time, run the following code. The temporaraly hard coded 'finalString' value works with the 24 removed from the end. After you run the code comment out the 'finalString' value being used and un comment the one above, this is the problematic string:

NOTE: the output has /06 appended to the end. The numbers being run through the function are what preceeds the forward slash.

<%
Function findMissing(str, delim)
    '' first split the string to get an array
    '' 2nd sort the array
    '' 3rd, look for the missing numbers in sequence, using mat41s method
    findMissing = ""
    ar = Split(str, delim)
    ar = sortIt(ar)
    For i = 0 To UBound(ar) - 1
        If CLng(ar(i)) + 1 <> CLng(ar(i + 1)) Then
            '' we found missing number(s)
            For j = 1 To (CLng(ar(i + 1)) - CLng(ar(i))) - 1
                findMissing = findMissing & CStr(CLng(ar(i)) + j) & ","
            Next
        End If
    Next
End Function

Function sortIt(ar)
For j = 0 To UBound(ar)
        For i = 0 To UBound(ar) - 1
            If CLng(ar(i)) > CLng(ar(i + 1)) Then
                tmp = ar(i)
                ar(i) = ar(i + 1)
                ar(i + 1) = tmp
                Exit For
            End If
        Next
    Next

    sortIt = ar
End Function


'finalString = "1,2,3,4,5,6,7,8,10,11,12,13,16,17,18,19,20,21,22, 25,26,27,28,29,31,32,33,35,37,38,36,30,9,39,40,42, 43,44,45,24"
finalString = "1,2,3,4,5,6,7,8,10,11,12,13,16,17,18,19,20,21,22, 25,26,27,28,29,31,32,33,35,37,38,36,30,9,39,40,42, 43,44,45"
If len(finalString) > 2 Then
   missingNumArray = split(findMissing(finalString, ","),",")
   For numCount2 = 0 To ubound(missingNumArray)-1
       response.write missingNumArray(numCount2) & "/0" & right(osirNum, 1) & ", "
   Next
   Set missingNumArray = nothing
End if
%>

Any ideas would be much appreciated TYIA



Wind is your friend
Matt
Reply With Quote
  #6 (permalink)  
Old February 12th, 2007, 06:04 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Matt,

I found that your sort method did not work correctly, replacing it with the following made the code work for me...
Code:
Function sortIt(ar)
    For j = 0 To UBound(ar) - 1
        For i = j + 1 To UBound(ar)
            If ar(i) - ar(j) < 0 Then
                tmp = ar(j)
                ar(j) = ar(i)
                ar(i) = tmp
            End If
        Next
    Next
    sortIt = ar
End Function
HTH,

Chris

Reply With Quote
  #7 (permalink)  
Old February 12th, 2007, 07:07 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Chris - Seeing your email this morning has made me a very happy camper. I spent to long, and got to frustrated at it yesturday. I will examine the differences and identify why the function was so week. FYI it works fine with small arrays however fails when the array is of medium size or greater.

You have a fine day and thanks again :0)

Wind is your friend
Matt
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To find the missing numbers in a list of cells yogeshyl Excel VBA 2 December 13th, 2007 02:02 AM
Compare two table - Find missing record lawsoncobol Access VBA 5 August 4th, 2006 01:18 AM
Anybody find the missing code in ch6? nickolas BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 January 29th, 2006 10:38 AM
Find out missing record hari-kumar-vadakkeveedu SQL Server 2000 3 October 15th, 2004 01:09 AM
find maximum number Abhinav_jain_mca SQL Server 2000 4 September 28th, 2004 08:13 AM



All times are GMT -4. The time now is 08:53 AM.


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