{"id":66553,"date":"2006-09-01T14:35:00","date_gmt":"2006-09-01T14:35:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/09\/01\/how-can-i-tell-which-numbers-are-missing-from-a-sequence-of-numbers-found-in-a-text-file\/"},"modified":"2006-09-01T14:35:00","modified_gmt":"2006-09-01T14:35:00","slug":"how-can-i-tell-which-numbers-are-missing-from-a-sequence-of-numbers-found-in-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-which-numbers-are-missing-from-a-sequence-of-numbers-found-in-a-text-file\/","title":{"rendered":"How Can I Tell Which Numbers Are Missing From a Sequence of Numbers Found in a Text File?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I look at a sequence of numbers in a text file and identify which numbers are missing from the sequence?<BR><BR>&#8212; TP<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, TP. Don\u2019t you know that it\u2019s Friday? This sounds like a tricky question to answer, and the Scripting Guys have an agreement with Microsoft that we don\u2019t have to think very hard on Fridays. OK, granted, Microsoft isn\u2019t aware that we have such an agreement. But let\u2019s not nitpick over details. The point is that there\u2019s absolutely no way we\u2019re going to answer this question, at least not today. Sorry, but that\u2019s just the way it goes.<\/P>\n<P>Oh, come on, TP; don\u2019t look at us with those sad, puppy dog eyes \u2013 OK, fine, we\u2019ll make a deal with you. We\u2019ll give you an answer to your question; however, this answer will be based on the assumption that the numbers in your text file are already in numerical order (that is, 1-2-3-4 and not 2-1-4-3). If the numbers are <I>not<\/I> in numerical order you\u2019ll need to sort them before the script we\u2019re about to show you will work. (How can you sort the contents of a text files? This <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/feb05\/hey0225.mspx\"><B>Hey Scripting Guy!<\/B><\/A> column offers one suggestion.)<\/P>\n<P>With that in mind, let\u2019s assume we have a text file containing the following sequence of numbers:<\/P><PRE class=\"codeSample\">2\n4\n8\n9\n10\n<\/PRE>\n<P>How are we going to determine which numbers (1, 3, 5, 6, and 7) are missing from the sequence? Like this:<\/P><PRE class=\"codeSample\">Const ForReading = 1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;c:\\scripts\\test.txt&#8221;)<\/p>\n<p>i = 1<\/p>\n<p>Do Until objFile.AtEndOfStream\n    intLine = CInt(objFile.ReadLine)<\/p>\n<p>    Do Until x = 1\n        If intLine = i Then\n            Exit Do\n        End If\n        Wscript.Echo i\n        i = i + 1\n    Loop\n    i = i + 1\nLoop<\/p>\n<p>objFile.Close\n<\/PRE>\n<P>What\u2019s that? Explain how this script works? Come on, TP; didn\u2019t we mention that it was <I>Friday<\/I>? <\/P>\n<P>Oh, all right. We start off by defining a constant named ForReading and setting the value to 1; we\u2019ll need this constant in order to open and read our text file. Next we create an instance of the <B>Scripting.FileSystemObject<\/B>, and then use the <B>OpenTextFile<\/B> method to open the file C:\\Scripts\\Test.txt.<\/P>\n<P>That brings us to this line of code:<\/P><PRE class=\"codeSample\">i = 1\n<\/PRE>\n<P>Sure, that <I>looks<\/I> like a meaningless line of code. In reality, though, it\u2019s the key to this entire script. We\u2019re going to use the counter variable i as a way to keep track of which number we <I>should<\/I> be on in our sequence. We set i to 1 because the first number in our sequence (and thus the first number in the text file) should be 1. If the first number in the text file is 1 <I>and<\/I> the value of i (the first number in the sequence) is 1 that means that the number 1 is present and accounted for. If the two items don\u2019t match then the number 1 must be missing.<\/P>\n<P>Don\u2019t worry; that will make more sense in a minute or so.<\/P>\n<P>After assigning a value to the counter variable we set up a Do Until loop that runs until the entire file has been read (or, if you want to impress your friends and neighbors, until the <B>AtEndOfStream <\/B>property is True). Inside that loop the first thing we do is read in the initial line from the text file and assign that value to a variable named intLine:<\/P><PRE class=\"codeSample\">intLine = CInt(objFile.ReadLine)\n<\/PRE>\n<TABLE id=\"ELE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. What\u2019s the <B>CInt<\/B> for? That\u2019s a VBScript function that converts character data to an integer value. We\u2019re doing that to make sure VBScript treats the line of text as a number and not as a string.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>We\u2019re now ready to see if the first number in the text file (which, in our example, happens to be 2) is equal to the first number in our sequence (1). To do that we set up a second Do Until loop, one that\u2019s designed to run until x is equal to 1. (Technically, x will <I>never<\/I> be equal to 1; this is just a little trick designed to keep the loop running until we explicitly tell it to stop.) Inside this second loop we use the following line of code to determine whether or not the value read from the text file (intLine) is equal to the first number in the sequence (the counter variable i):<\/P><PRE class=\"codeSample\">If intLine = i Then\n<\/PRE>\n<P>As you probably know, 2 is <I>not<\/I> equal to 1. (No, really: we wouldn\u2019t kid you about that.) Therefore, we do these two things:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>We echo the value of i. Why? Because the value of i (which, the first time through the loop, is equal to 1) is <I>not<\/I> in the text file.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>After reporting that the value 1 was not found we then increment the value of i by 1. That makes our counter variable equal to 2 (1 + 1), which is, of course, the next value in the sequence.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>We then loop around and check to see if the value read from the text file (we\u2019re still working with the first line in the text file, so the value of intLine is still 2) is equal to the new value assigned to the counter variable (2). In this case the two numbers <I>are<\/I> equal; therefore, we don\u2019t want to echo the value because the value (2) <I>does<\/I> exist in the text file. Instead we call the <B>Exit Do<\/B> statement to exit the secondary loop. Having found what we were looking for (the value 2) we\u2019re ready to move on.<\/P>\n<P>The Exit Do statement pops us back to the main loop; from there we first increment i (which now gives i a value of 3), read the second line in the text file and repeat the process all over again. The second time around the value read from the text file (4) will not be equal the value of the counter variable (which, as you recall, is now set to 3). Therefore, we echo 3, increment the counter (to 4) and check <I>that<\/I> against the value from the text file (also a 4). <\/P>\n<P>Etc., etc.<\/P>\n<P>When all is said and done we should get back a \u201creport\u201d that looks like this:<\/P><PRE class=\"codeSample\">1\n3\n5\n6\n7\n<\/PRE>\n<P>Hope that helps, TP. And, now seeing as how it <I>is<\/I> Friday we \u2013 what\u2019s that? No, we are <I>not<\/I> going to wash your car for you. No, sorry; we have to draw the line somewhere. And don\u2019t look at us with those sad puppy dog eyes, don\u2019t \u2013 fine; we\u2019ll go get a bucket and the hose. <\/P>\n<P>So much for TGIF.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I look at a sequence of numbers in a text file and identify which numbers are missing from the sequence?&#8212; TP Hey, TP. Don\u2019t you know that it\u2019s Friday? This sounds like a tricky question to answer, and the Scripting Guys have an agreement with Microsoft that we don\u2019t have [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,14,5],"class_list":["post-66553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I look at a sequence of numbers in a text file and identify which numbers are missing from the sequence?&#8212; TP Hey, TP. Don\u2019t you know that it\u2019s Friday? This sounds like a tricky question to answer, and the Scripting Guys have an agreement with Microsoft that we don\u2019t have [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66553"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66553\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}