{"id":69623,"date":"2005-06-10T11:28:00","date_gmt":"2005-06-10T11:28:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/06\/10\/how-can-i-determine-the-number-of-items-generated-by-the-split-function\/"},"modified":"2005-06-10T11:28:00","modified_gmt":"2005-06-10T11:28:00","slug":"how-can-i-determine-the-number-of-items-generated-by-the-split-function","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-number-of-items-generated-by-the-split-function\/","title":{"rendered":"How Can I Determine the Number of Items Generated by the Split Function?"},"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! I know I can use the Split function to split a line into individual items. But how can I tell how many \u201csplits\u201d I ended up with; that is, how many individual items did I generate from a single line?<BR><BR>&#8212; SA<\/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, SA. For those of you who are relative newcomers to scripting (or those of you who\u2019ve never used the Split function) let\u2019s start off with a brief tutorial. As SA notes, the <B>Split<\/B> function lets you take a text string of one or more items (with individual items separated by a common delimiter, such as a comma) and then create an array of individual items. For example, suppose we have this line of text:<\/P><PRE class=\"codeSample\">Dopey,Sneezy,Doc,Grumpy,Sleepy,Happy,Bashful\n<\/PRE>\n<P>By running the Split function and telling the function that the comma is our delimiter (that is, the character that separates the individual items) we can easily separate this string into its constituent parts:<\/P><PRE class=\"codeSample\">Dopey\nSneezy\nDoc\nGrumpy\nSleepy\nHappy\nBashful\n<\/PRE>\n<P>All in all a very handy little function, especially when working with file paths, Active Directory AdsPaths, and anything else where the parts are often-times every bit as important as the whole. (For example, the Split function can take a file path like C:\\Scripts\\MyScript.vbs and separate out the drive, folder, and file name for us.)<\/P>\n<P>All well and good. But SA would like to know how many items were found in an individual text string; for example, we don\u2019t want to know the names of the Seven Dwarves, we just want to know that seven dwarves were listed in the string. <\/P>\n<TABLE id=\"EBD\" 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>. At the risk of bragging, we might point out that the Scripting Guys &#8211; who are often mistaken for the Seven Dwarves &#8211; knew all seven names <I>without<\/I> looking them up. And here\u2019s something for you trivia buffs: Dopey (the dwarf most often confused with the Scripting Guys) was originally named Deafy. Strange but true!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So how can we determine the number of items found in the string? Well, we just use a simple little script like this:<\/P><PRE class=\"codeSample\">strNames = &#8220;Dopey,Sneezy,Doc,Grumpy,Sleepy,Happy,Bashful&#8221;\narrNames = Split(strNames, &#8220;,&#8221;)\nWscript.Echo Ubound(arrNames) + 1\n<\/PRE>\n<P>In line 1 we assign the names of our seven dwarves (each name separated by a comma) to a variable named strNames. In line 2 we call the Split function and indicate that the delimiter is the comma. That\u2019s why you see <B>\u201c,\u201d<\/B> as the second parameter passed to Split; the first parameter, of course, is the string we want Split to work against. This gives us back an array named arrNames, with each item in the array representing the name of one of the dwarves. (Incidentally, the name <I>arrNames<\/I> is purely arbitrary; you can use any variable name you want.)<\/P>\n<P>That brings us to line 3:<\/P><PRE class=\"codeSample\">Wscript.Echo Ubound(arrNames) + 1\n<\/PRE>\n<P>What we\u2019re doing here is echoing the <B>Ubound<\/B> (upper bound) value of our array, plus 1. What does that mean? Well, in an array each item is automatically assigned an index number: the first item is assigned index number 0, the second item is assigned index number 1, and so on. In memory, our array looks something like this:<\/P>\n<TABLE id=\"EAE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Index Number<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Item<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">0<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Dopey<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">1<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Sneezy<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">2<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Doc<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">3<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Grumpy<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">4<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Sleepy<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">5<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Happy<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">6<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Bashful<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>The Ubound function will always return the index number of the last item in the array; in this case, Ubound returns a 6. The Ubound value will always be one less than the number of items in the array; remember, we actually have <I>seven<\/I> items in our array. This occurs because the first item in the array is assigned an index number of 0; if the first item was number 1 then the Ubound value would also represent the total number of items in the array. But that\u2019s OK: all we have to do is take the Ubound value, add 1 to it, and we\u2019ll have our total number of items. Case closed!<\/P>\n<P>Oh, and one other thing: yes, we will <I>strongly<\/I> discourage Peter from singing a scripting-related version of <I>Someday My Prince Will Come<\/I> during his next webcast. You have our word on it.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I know I can use the Split function to split a line into individual items. But how can I tell how many \u201csplits\u201d I ended up with; that is, how many individual items did I generate from a single line?&#8212; SA Hey, SA. For those of you who are relative newcomers to [&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":[18,3,4,5],"class_list":["post-69623","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-arrays-hash-tables-and-dictionary-objects","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I know I can use the Split function to split a line into individual items. But how can I tell how many \u201csplits\u201d I ended up with; that is, how many individual items did I generate from a single line?&#8212; SA Hey, SA. For those of you who are relative newcomers to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69623","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=69623"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69623\/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=69623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}