This is my first post here but reading other posts has helped me with countless issues. You guys are awesome. So thanks for all the help you guys have provided up to this point and thanks in advance for any advice you can give me here.
So, I’m working on this script that will end up randomly choosing numbers from a list in an array. What I’m trying to do in this section is read the number in each element of the third dimension of NumberHistoryArray and write the corresponding number from the second dimension the number of times indicated in the third into the numberDump array. I also have it writing to a text file just to see that it works and to have something to reference if I run into other problems and need to know what the output is.
Edit
The second dimension of NumberHistoryArray contains the numbers 1 through 35. The third dimension contains the number of times each of the numbers in the second dimension should be repeated when written to the numberDump array.
If NumberHistoryArray(4, 0, 0) is 4, for example, it should write the number 1 four times, once each in numberDump(0, 0, 0) through (0, 0, 3) and dynamically resize numberDump() as needed to fit each 1. The result of echoing numberDump(0, 0, 0) through numberDump(0, 0, 3) should be 1 1 1 1. If NumberHistoryArray(4, 1, 1) is 7, it should write the number 2 seven times in numberDump(1, 1, 0).
Edit
When writing to the numberDump text file from within the Do loop, the text file is perfect. If I echo the contents of each element in the array from inside the Do loop, the contents are accurate. Once outside the Do loop, the contents are blank. Any takers?
Dim NumberHistoryArray(5, 34, 3)
Dim numberDump()
reDim numberDump(3, 34, 0)
' generate the number list and store it in the array
For n = LBound(numberDump, 1) to UBound(numberDump, 1)
i = 1
x = 0
y = 1
Do
Set objTextFile = objFSO.OpenTextFile("numberDump.txt", 8, True)
' determine how many elements are needed in the third dimension of the
' array to store the output
x = x + NumberHistoryArray(4, i - 1, n)
' resize the array
ReDim Preserve numberDump(3, 34, x)
' input the numbers
For z = y to UBound(NumberDump, 3)
numberDump(n, i - 1, z) = i
objTextFile.WriteLine(numberDump(n, i - 1, z))
Next
objTextFile.Close
y = x + 1
i = i + 1
Loop Until i = 36
Next
For i = 0 to UBound(numberDump, 2)
For j = 0 to UBound(numberDump, 3)
wscript.Echo numberDump(0, i, j)
Next
Next
The issue here is that you are resizing the third dimension of
numberDumptoxeach iteration, and you are resetting the value ofxwith each iteration ofn. For example, whennis 0, by the end of theDoloop,xmay be equal to 10. However, on the next iteration, whennis 1,xis reset to 0 and you end up resizing the third dimension ofnumberDumpto 0, erasing most of the existing elements.There are two methods of working around this:
Keep track of the maximum value of
xfor all iterations ofn. Use that value to resizenumberDumpas necessary.Use a jagged array (aka, an array of arrays). In this case,
numberDumpwould be a two-dimensional array whose values are arrays containing the repeated values. This would allow each value to have a different length, as opposed to a three-dimensional array, where the third dimension always has the same length.