Naiya55
Naiya55

Reputation: 134

ASP Classic How to change the length of an array that is held by another array (not true 2d array)

I'm need to be able to have a 2d array, where the length of second array varies on a case by case basis. To this end, I made an array that contains other arrays with the following code:

 Dim timeline
    ReDim timeline(days)

    for reDate = beginDate to endDate 
        timeline(DateDiff("d", beginDate, reDate)) = Array(0)
    next

The problem I am having is that I am unable to change the size of any of the arrays contained by timeline, as ReDim doesn't seem to work when I do this. Does anyone know how I would go about this?

Upvotes: 2

Views: 1538

Answers (2)

omegastripes
omegastripes

Reputation: 12612

Try the below snippet, using a as temporary array variable:

Dim timeline, a
ReDim timeline(days)

' initial fill the array
For reDate = beginDate to endDate
    a = Array()
    ReDim a(99)
    timeline(DateDiff("d", beginDate, reDate)) = a
Next

' redim sub arrays
For reDate = beginDate to endDate
    ' assign subarray to a
    a = timeline(DateDiff("d", beginDate, reDate))
    ' redim a
    ReDim Preserve a(199)
    ' put changed array into root array
    timeline(DateDiff("d", beginDate, reDate)) = a
Next

In this case each subarray contains 100 elements first, and 200 after redim.

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38755

"Does not work" and error messages ("Object required") without code/context are not the best way to ask a question. The first is a complete waste of time; the second may indicate that you used Set where you shouldn't (VBScript Arrays are not objects, so there shouldn't be any Set in the code).

This demonstrates the same facts that @omegastripes pointed out, but gives hints wrt possible pitfalls:

Option Explicit

Dim AoA : AoA = Split("s o m e|w o r d s|o f|d i f f e r e n t|l e n g t h", "|")
Dim i
For i = 0 To UBound(AoA)
    AoA(i) = Split(AoA(i))
Next
WScript.Echo "----- test data:"
For i = 0 To UBound(AoA)
    WScript.Echo Join(AoA(i), "")
Next
WScript.Echo "----- array assignment copies (For Each elem ... does not 'work'):"
Dim e
For Each e In AoA
    e = "zap"
Next
For Each e In AoA
    WScript.Echo Join(e, "")
Next
WScript.Echo "----- array assignment copies (change needs index access):"
For i = 0 To UBound(AoA)
    e = AoA(i)
    e(0) = UCase(e(0)) ' just changes the copy (e)
    WScript.Echo Join(e, "")
    AoA(i)(1) = UCase(AoA(i)(1)) ' access via indices changes org collection
    WScript.Echo Join(AoA(i), "")
Next
WScript.Echo "----- replace whole subarrays (re-dimensioned dynamically):"
Dim n, m
For i = 0 To UBound(AoA)
    e = AoA(i)
    n = UBound(e)
    ReDim Preserve e(n * 2 + 1)
    For m = n + 1 To UBound(e)
        e(m) = UCase(e(m - n - 1))
    Next
    AoA(i) = e ' replace whole element
    WScript.Echo Join(AoA(i), "")
Next

output:

cscript 37951664.vbs
----- test data:
some
words
of
different
length
----- array assignment copies (For Each elem ... does not 'work'):
some
words
of
different
length
----- array assignment copies:
Some
sOme
Words
wOrds
Of
oF
Different
dIfferent
Length
lEngth
----- replace whole subarrays:
sOmeSOME
wOrdsWORDS
oFOF
dIfferentDIFFERENT
lEngthLENGTH

Upvotes: 0

Related Questions