Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

redim preserve a 2-dimensional array

Status
Not open for further replies.

deetee2k

Programmer
Nov 25, 2000
90
IE
I've an array mu(MAX_1,MAX_2) where MAX_1=100,MAX_2=7

However once I've assigned calculations to it it may turn out that the first index is 2 and the second is 5.

How do I redim preserve such an array? If I leave it it just prints out about 700 zeros, and I know that you can only redim preserve the second index in a 2-dim array.

Thanks

D.
 
A way to do it is to create a dynamic array var, run your procedure(s). When you know the correct dimensions, Redim the second array (to the correct dimensions) and copy the (valid) contents of the first array to the second.

Variations on this theme abound!

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 

Hi,

You can also make arrays of arrays! in That way you dont even need the same number of columns in each rows.

----------------------------------------------------------
Dim Arr1() As Variant, Arr2() As String

ReDim Arr1(0)
ReDim Arr2(1)
Arr2(0) = "hello": Arr2(1) = "Goodday"
Arr1(0) = Arr2
ReDim Arr2(2)
Arr2(0) = "bye": Arr2(1) = "see you": Arr2(1) = "goodbye"
ReDim Preserve Arr1(1)
Arr1(1) = Arr2
MsgBox (Arr1(0)(0))
----------------------------------------------------------

In this way you can make complex data structures of any dimesions with elements of any length -it really cool.






Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Sunaj,

Do you REALLY think deetee2000 can make use of your 'really cool' explination? MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
thanks guys (thanks again sunaj!),

michaelred I can't see what you mean, could you give a short code example?

 
Below is a crude version of the concept. In this example, an arbitrary text file is read from disc in one "Chunk". It is seperated into an array of "Lines", then each line is seperated into an array of "Words". The arrays or words are placed into the temporary array with the 'fixed'dimensions (100, 7).

At the conclusion of populating the temporary array, we "know" the actual dimensions of your "Mu" array, and redimension it appropiatly and use a simply loop nest to populate it with the viable contents of the fixed array.

Obviously, this is only a simplistic example. The same effect could be achieved in many other ways, and in any real production app, some error checking needs to be incorporated.


Code:
Public Function basNewArray(filIn As String) As Variant

    Dim FilId As Integer
    Dim Idx As Integer
    Dim Jdx As Integer
    Dim Kdx As Integer
    Dim RawFile As String
    Dim MyLines As Variant
    Dim MyWords As Variant
    Dim TempArray(100, 7) As Variant
    Dim Mu() As Variant

    'I 've an array mu(MAX_1,MAX_2) where MAX_1=100,MAX_2=7

    'However once I've assigned calculations to it it may turn out that
    'the first index is 2 and the second is 5.

    'How do I redim preserve such an array?
    'If I leave it it just prints out about 700 zeros, and I know that
    'you can only redim preserve the second index in a 2-dim array.


    'SAMPLE Usage:
    '? basNewArray("C:\My Documents\Fleas.Txt")

    'C:\My Documents\Fleas.Txt is listed below
    'My Dog Has Fleas
    'Your Dog Has Fleas
    'His Dog Has Fleas
    'All Dogs Have Fleas
    'This Cat Has Fleas
    'That Cat Has Fleas
    'Those Cats Have Fleas
    'Who Doesn 't Have Fleas
    'I Don 't Have Fleas


    FilId = FreeFile                    'File Handle
    Open filIn For Binary As FilId      'Open File

    RawFile = Space(LOF(FilId))         'Assign File String proper Length
    Get #FilId, 1, RawFile              'Get File into String

    MyLines = Split(RawFile, vbCrLf)    'Split file into an Array of Strings

    Idx = 0                             'Initalize Index for String array
    Jdx = 0                             'Initalize the "Mu" ArrayFirst Index (100)
    Do While Idx <= UBound(MyLines)     'Loop structure for array of Lines
    
        MyWords = Split(MyLines(Idx))   'Split the line into an array or &quot;Words&quot;

        Kdx = 0                         'Initalize the second Index (7) for &quot;Mu&quot;
        Do While Kdx <= UBound(MyWords)

            TempArray(Idx, Kdx) = MyWords(Kdx) 'Populate the Temp Array
            Kdx = Kdx + 1
            Jdx = basMaxVal(Jdx, Kdx)      'Keep Max value for &quot;Word&quot; index
        Loop

        Idx = Idx + 1                   'Increment the Lines array Index
    Loop
    
    'Adjust the Idx TO the TRUE Index Value
    Idx = Idx - 1

    'Arrive Here w/
    'Idx ~ Max for the First Array Index AND
    'Jdx ~ Max for the Second array Index

    ReDim Mu(Idx, Jdx)                  'Re-Dim the &quot;Mu&quot; Array w/ Proper dimensions

    Idx = 0
    Do While Idx <= UBound(Mu, 1)
    
        Jdx = 0
        Do While Jdx <= UBound(Mu, 2)
            Mu(Idx, Jdx) = TempArray(Idx, Jdx)
        
            Jdx = Jdx + 1
        Loop

        Idx = Idx + 1
    Loop

End Function



MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top