Link to home
Start Free TrialLog in
Avatar of shersu
shersu

asked on

Delimited text file to 2 dimensional array

Hi,

I have a text file that contains two columns seperated by a delimiter(;).
I would like to store the values into a two dimensional array, so that I can retrieve one value from the array and it's corresponding value from the same array for furthur processing. Can this be accomplished?

Thanks
Avatar of eekj
eekj

the answer depends on what language are youre using.
Avatar of shersu

ASKER

Oh, sorry! I use VB6.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is a small piece of code I flung together... It's not optimal, but it works fast enough if you file isn't to big. I'm not to clued up on multi dimentional arrays, that's why I didn't add both items to the array in one go... If someone would like to optomize the code, I'd like to see how too...

KlockWurk

    Dim tmpArray1() As String
    Dim tmpArray2() As String
    Dim tmpArrayFinal() As String
   
    Dim tmpArrayOnly2 As Variant
    Dim nCounter As Integer
   
    Dim FileName As String
   
    FileName = "C:\test.txt"

    nCounter = 0
    Dim intFileNum As Integer
    Dim strText As String
    Dim strTemp As String
   
    ' Get the File Number
    intFileNum = FreeFile
   
    ' Open the File
    Open FileName For Input As #intFileNum
   
    ' Loop through each line, appending to the textbox
    Do Until EOF(intFileNum)
        ' Keep Windows Breathing
        DoEvents
        ' Get the data
        Line Input #intFileNum, strTemp
       
        tmpArrayOnly2 = Split(strTemp, ";")
        If nCounter = 0 Then
            ReDim tmpArray1(1)
            ReDim tmpArray2(1)
        Else
            ReDim Preserve tmpArray1(nCounter)
            ReDim Preserve tmpArray2(nCounter)
        End If
        tmpArray1(nCounter) = tmpArrayOnly2(0)
        tmpArray2(nCounter) = tmpArrayOnly2(1)
        nCounter = nCounter + 1
    Loop
   
    ' Close file
    Close #intFileNum

    nCounter = UBound(tmpArray1)
    ReDim tmpArrayFinal(0 To nCounter - 1, 0 To 1)
    Dim x As Integer
    For x = 0 To nCounter - 1
        tmpArrayFinal(x, 0) = tmpArray1(x)
        tmpArrayFinal(x, 1) = tmpArray2(x)
    Next x
Avatar of shersu

ASKER

Thanks all for your help.