title: Adding Values to a dynamic array in vbscript from keys of dictionary object
body: <p>I am a beginner to VBScript and new to SO and i am trying to add Keys of a dictionary object to a dynamic array based on certain condition that an array should hold maximum cnt of 100 or less than 100 only.Please find the below code and explanation for more clarity.</p>
  <pre><code>Dim dict,dict1,str_nbr,cnt,arrtest2()
  
  str_nbr = &quot;9728&quot;
  
  Set dict = CreateObject(&quot;Scripting.Dictionary&quot;)
  dict.Add &quot;Returns&quot;,&quot;67&quot;
  dict.Add &quot;Debit&quot;,&quot;59&quot;
  dict.Add &quot;PSR_PQV&quot;,&quot;55&quot;
  dict.Add &quot;GiftCard&quot;,&quot;54&quot;
  dict.Add &quot;Sales&quot;,&quot;45&quot;
  dict.Add &quot;Discounts&quot;,&quot;26&quot;
  dict.Add &quot;WIP&quot;,&quot;25&quot;
  dict.Add &quot;Pro&quot;,&quot;23&quot;
  dict.Add &quot;Void&quot;,&quot;20&quot;
  dict.Add &quot;Receipt&quot;,&quot;15&quot;
  dict.Add &quot;Price&quot;,&quot;12&quot;
  dict.Add &quot;Inquiry&quot;,&quot;6&quot;
  
  
  dictItems = dict.Items
  dictKeys = dict.Keys
  
  ABC =  CreateTestArray(dictItems,arrtest2)
  </code></pre>
  <p><strong>The issue i am facing is with function CreateTestArray().Please find the 2 Functions used Below,</strong></p>
  <pre><code>Function CreateTestArray(dictObj,arrTest)
      sum =0 
  arrcnt = 0
  For i = 0 To ubound(dictObj)
      val = dictObj(i)
      'arrcnt = 0
      Do 
          'arrcnt = 0
          Total = sum + val
          val1 = arrcnt + total
              If (Total&lt;=100) and (val1&lt;=100)Then
                  
              ReDim  preserve arrTest(i)
                  arrTest(0) = str_nbr
                  call AddItem(arrTest,dictKeys(i))
                  dict.remove(dictKeys(i))
                  arrcnt = val1
                  Exit do
              else
                  Exit do
              End If
              'arrcnt = val1
      Loop while (arrcnt&lt;=100)
      If arrcnt&gt;95 and arrcnt&lt;=100 Then
          Exit for
      End If
  Next
  CreateTestArray = arrTest
  End Function
  </code></pre>
  <p>'####################################</p>
  <pre><code> 'To add value to array
      
  Function AddItem(arr, val)
      
          ReDim Preserve arr(UBound(arr) + 1)
          arr(UBound(arr)) = val
          AddItem = arr
         
      End Function
  </code></pre>
  <p>'####################################</p>
  <p>The purpose of the function CreateTestArray(dictObj,arrTest) is that it will iterate the Items of the dictionary object and sum with each other till the arrcnt &lt;=100 and add it to a array and remove the added key to avoid duplicates when the function is called next time.For Example in the dictionary provided,if we sum the values of Returns,Discounts,Inquiry(67+26+6 = 99),Then arrcnt = 99 which is less than equal to 100 and more than 95 as per requirement.So i am trying to add the keys(Returns,Discounts,Inquiry) to a dynamic array,having **<strong>str_nbr at index '0' as per requirement</strong> and adding the keys (Returns,Discounts,Inquiry) subsequently to the same array.The output of the  array i receive is like below:</p>
  <pre><code>**output:**
  ABC(0) - 9728
  ABC(1) - Returns
  ABC(2) - EMPTY
  ABC(3) - EMPTY
  ABC(4) - EMPTY
  ABC(5) - EMPTY
  ABC(6) - Discounts
  ABC(7) - EMPTY
  ABC(8) - EMPTY
  ABC(9) -  EMPTY
  ABC(10) - EMPTY
  ABC(11) - EMPTY
  ABC(12) - Inquiry
  </code></pre>
  <p>Please suggest a way or workaround <strong>to avoid the 'Empty' fields</strong> in the array without changing the remaining logic,I have tried many approaches to get this right but nothing worked out.My final output should be an array just having (9728,returns,discounts,inquiry) omiting the empty fields.Kindly help me out in acheiving this which will be really helpful to proceed further.Thanks in advance.</p>