Results 1 to 6 of 6

Thread: splitting a text file into 2 separate arrays

  1. #1

    Thread Starter
    Addicted Member run_GMoney's Avatar
    Join Date
    May 2002
    Location
    Detroit
    Posts
    186

    splitting a text file into 2 separate arrays

    How do I go about splitting the data if I've got 2 columns of data, and I need the first column in one array and the second column in a different array?

  2. #2
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    Are both columns separated in any way? A tab is already sufficient.
    You can use the VB function Split in this case. This will give you an array of all values separated by the separator.
    Syntax: array = split(line, separator) (could be vbTab)
    Later you can do with the values in the array whatever you want, of course.

  3. #3

    Thread Starter
    Addicted Member run_GMoney's Avatar
    Join Date
    May 2002
    Location
    Detroit
    Posts
    186
    So would that mean that my first array would be all of the even values in the array (array(0), array(2), etc) and the second array would be all the odd values (array(1), array(3), etc)? Then would it be practical to loop through the array and send the evens one way and the odds the other way?

  4. #4
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    No, you'll just get an array with only two values (when the separator only occurs once per line)
    The value array(0) should be appended to the first array, and the value array(1) to the second.

    I'll write some (pseudo-)code for your app, if you're interested
    VB Code:
    1. 'Declaration of variables
    2. dim lenoffile as long
    3. dim buffer as string
    4. dim lines() as string
    5. dim numlines as long
    6. dim firstcolumn() as string
    7. dim secondcolumn() as string
    8. dim i as long
    9.  
    10. 'Open textfile in binary mode. Why should you read line by line? This way saves you time, supposed that you have enough memory
    11. open "file.txt" for binary as #1
    12. lenoffile = lof(1)   'Length of file
    13. buffer = space$(lenoffile)   'Make the buffer large enough
    14. get #1, , buffer   'Get the data into the buffer
    15. close #1   'you don't need it anymore
    16. lines = split(buffer, vbcrlf)  'Split the buffer into lines
    17. numlines = ubound(lines) + 1   'Determine the number of lines
    18.  
    19. redim firstcolumn(numlines - 1)
    20. redim secondcolumn(numlines - 1)
    21.  
    22. 'I've explained this part in my first post
    23. for i = 0 to numlines - 1
    24.   tempbuf = split(lines(i), vbtab)
    25.   firstcolumn(i) = tempbuf(0)
    26.   secondcolumn(i) = tempbuf(1)
    27. next i
    28. 'done!
    Assumptions:
    *The lbound of arrays is 0 (hence the -1's every where)
    *Firstcolumn and secondcolumn are the arrays where you wish to store the values.
    *The textfile contains only data. Two values, separated by a tab, per line

    Don't be scared by the binary opening of the file, and the get. I think this is just a much faster way than read a file in text mode line by line.

    Good luck!

    [edit]Added some comments[/edit]

  5. #5

    Thread Starter
    Addicted Member run_GMoney's Avatar
    Join Date
    May 2002
    Location
    Detroit
    Posts
    186
    OK, so the resulting array is only 2 items? array(0) would be all of the values in the first column and array(1) would be all the values of the second column if I used the following code...

    VB Code:
    1. Dim array() as String
    2.  
    3. Open "C:\textfile.txt" For Input As #1
    4.   array = Split(Input(LOF(1), 1) , vbTab
    5. Close #1

    Is that basically what you're saying. If so, then I guess it's pretty easy from there. But how would each item in the array then be separated? Assuming I have 10 rows, that would be 10 items in array(0) correct? I'm still a bit confused on how it's separating the data.

  6. #6
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    The resulting array of this statement, tempbuf = split(lines(i), vbtab), will contain two items. For every line the tempbuf-array will hold two items. It's your responsibility to grab those values into the two arrays you want to store your values in (in this case firstcolumn and secondcolumn)

    By the way, I forgot to declare tempbuf. It's : Dim tempbuf() as string

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