Results 1 to 15 of 15

Thread: Two Dimentional Array and Split? Stumped.

  1. #1
    fntzlnd
    Guest

    Two Dimentional Array and Split? Stumped.

    I have tried to find an already posted answer to this, but I could not so here goes...

    I have a text file that looks like this:

    header info line1
    header info line 2
    header info line 3
    9.880662e-001 1.815209e-001 -1.414392e+000 -5.080986e-001 1.729634e-001 -1.791693e-001
    9.880662e-001 1.815209e-001 -1.414392e+000 -5.080986e-001 1.729634e-001 -1.791693e-001 -9.064005e-002
    9.880662e-001 1.815209e-001 -1.414392e+000 -5.080986e-001 1.729634e-001 -1.791693e-001 -9.064005e-002
    9.880662e-001 1.815209e-001 -1.414392e+000 -5.080986e-001 1.729634e-001 -1.791693e-001 -9.064005e-002
    9.880662e-001 1.815209e-001 -1.414392e+000 -5.080986e-001 1.729634e-001 -1.791693e-001 -9.064005e-002
    9.880662e-001 1.815209e-001 -1.414392e+000 -5.080986e-001 1.729634e-001 -1.791693e-001 -9.064005e-002


    I have truncated the real length of the text file to fit in the message board, but I do know how many values are across and how many lines down.

    What I want to do is stuff these values into a two dimentional array.

    I have this code so far:

    Dim myArray() as Double
    Dim i as Integer

    Open CommonDialog1.FileName For Input As #1
    Dim lineNum As Integer
    Do Until EOF(1)
    Line Input #1, sLine
    lineNum = lineNum + 1
    If lineNum > 3 Then
    For i = 1 To valuesAcross
    myArray(lineNum - 3, 1) = Split(CDbl(sLine), " ")
    Next i
    End If
    Loop
    Close #1

    Its the poulating of the array that has me stumped. Can you please help. I am new to arrays. I understand how they work, but I am new to actually using them

    Thank you.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I don't think this is possible with two dimensional arrays.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    You could do this:

    VB Code:
    1. Dim lns() As String, tmp() As String
    2.  
    3.   tmp = Split("adf adsf adf adsf adsf adsf", " ")
    4.  
    5.   ReDim lns(1, UBound(tmp))
    6.   For i = 0 To UBound(tmp)
    7.     lns(1, i) = tmp(i)
    8.   Next

    But you've stated that the file is large, so that might not be a good idea with the loop. I believe there's an API function to do this, CopyMemory, maybe?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    joan_fl
    Guest
    The UBound command will give the count +1 of the number of elements in the array.

    For example.. to iterate through the array you would do something like this:

    VB Code:
    1. Dim iCount          As Integer
    2. Dim iCurr           As Integer
    3. Dim arrSomeArray()  As String
    4.  
    5.  
    6. arrSomeArray = Split("1 2 3 4 5", " ")
    7.  
    8.  
    9. iCount = UBound(arrSomeArray)
    10.  
    11. For iCurr = 0 To iCount
    12.     Debug.Print arrSomeArray(iCurr)
    13. Next iCurr

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by joan_fl
    The UBound command will give the count +1 of the number of elements in the array.

    For example.. to iterate through the array you would do something like this:
    That's not the kind of array he wanted. And the Ubound will give the count - 1 of items in the array. Ubound will give the upperbound. As in arrTemp(0 to 19) will give 19.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    fntzlnd: Also, you must declare (Dim) the size of the array, otherwise you'll get subscript out of range errors.

    Ex:

    VB Code:
    1. Dim arr() As Integer
    2.  
    3. arr(0) = 1 'this is bad
    4.  
    5. 'do this
    6. Redim arr(0)
    7. arr(0) = 1
    8.  
    9. 'if the array already has data, do
    10. Redim Preserve arr(1)
    11. arr(1) = 2

    However, for two dimensional arrays, there's different rules to follow when Redimming.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    fntzlnd
    Guest

    THANKS

    Thanks Hobo,

    That idea helped alot.

  8. #8
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Dim MyArray() as variant

    Loop with a loopvariable and read one line at a time:
    MyArray(LoopVar) = ReadNextLine()

    In ReadNextLine, create an array and read all five across elements in to it then, assign the 5 dimensional array to readline before the function ends.

    Then, each MyArray will contain an array of the items across.

    I'll give you code in a minute.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  9. #9
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Lord_Rat
    Dim MyArray() as variant

    Loop with a loopvariable and read one line at a time:
    MyArray(LoopVar) = ReadNextLine()

    In ReadNextLine, create an array and read all five across elements in to it then, assign the 5 dimensional array to readline before the function ends.

    Then, each MyArray will contain an array of the items across.

    I'll give you code in a minute.
    You're either really smart, or don't know what you're talking about. Because all of that just flew right by me.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    VB Code:
    1. Option Explicit
    2.  
    3. Dim MyArray() As Variant
    4.  
    5. Dim NumRows As Long
    6. Dim NumCols As Long
    7.  
    8. Private Sub Form_Load()
    9.    
    10.     'arbitrary
    11.     NumRows = 10
    12.     NumCols = 5
    13.    
    14.     Dim loopvar As Long
    15.     loopvar = 0
    16.    
    17.     ReDim MyArray(NumRows)
    18.    
    19.     For loopvar = 0 To NumRows
    20.         MyArray(loopvar) = ReadNextLine
    21.     Next
    22.    
    23.    
    24.     'next line is number of rows down by what column over (which seems backward, I know)
    25.     MsgBox MyArray(2)(3)
    26. End Sub
    27.  
    28. Private Function ReadNextLine()
    29.     Dim TempArray() As String
    30.     ReDim TempArray(NumCols)
    31.    
    32.     Dim loopvar As Long
    33.        
    34.     'if using a file for input, the next section would be
    35.     'replaced with a read of the elements in the file
    36.     For loopvar = 0 To NumCols
    37.         TempArray(loopvar) = Chr(64 + loopvar)
    38.     Next
    39.    
    40.     ReadNextLine = TempArray
    41. End Function
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  11. #11
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Do you understand the code?
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I'm clueless.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    A variant data type is capable of storing almost any data type available to Visual Basic.

    This includes arrays.

    Thus, you can create an array of variants and store in each array element another array.

    Get ready for the next part.

    Each element's array does not have to match the same array dimensions of the other elements.

    Thus, if

    MyArray(1) had an array of 5 elements,
    MyArray(2) could have an array of 6 elements.

    It makes no difference.

    My recommendation is to put a break point on the last line in the form load code above.

    When you reach that line, add my array as a watch item.

    You will see that when you expand it, you get a list of the elements of the MyArray, but then each element can be further expanded to show another array at a different level.

    In order to access array elements built this way, you access them with sets of parentheses

    (toplevel)(nextlevel)...(andSoOn)...(LastEmbeddedArray)

    The LastEmbeddedArray can be of any type, but all arrays above it must be variants.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  14. #14
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I just get a headache trying to think about it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  15. #15
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Lol

    Rarely would you need a data type of that design, but this is a case where it would make your code much simpler.

    If it causes a migraine, then forget about it and pretend this never happened.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width