 |
| 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 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
|
|
|
|

May 16th, 2006, 09:44 PM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
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
|
|

May 24th, 2006, 11:22 PM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have never heard of an intrinsic vbscript function to do this.
Woody Z http://www.learntoprogramnow.com
|
|

May 24th, 2006, 11:47 PM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
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
|
|

February 12th, 2007, 02:13 AM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
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
|
|

February 12th, 2007, 04:44 AM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
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
|
|

February 12th, 2007, 06:04 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

February 12th, 2007, 07:07 PM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
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
|
|
 |