|
-
Oct 30th, 2002, 02:19 PM
#1
Thread Starter
Addicted Member
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?
-
Oct 30th, 2002, 02:24 PM
#2
Fanatic Member
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.
-
Oct 30th, 2002, 02:30 PM
#3
Thread Starter
Addicted Member
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?
-
Oct 30th, 2002, 02:43 PM
#4
Fanatic Member
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:
'Declaration of variables
dim lenoffile as long
dim buffer as string
dim lines() as string
dim numlines as long
dim firstcolumn() as string
dim secondcolumn() as string
dim i as long
'Open textfile in binary mode. Why should you read line by line? This way saves you time, supposed that you have enough memory
open "file.txt" for binary as #1
lenoffile = lof(1) 'Length of file
buffer = space$(lenoffile) 'Make the buffer large enough
get #1, , buffer 'Get the data into the buffer
close #1 'you don't need it anymore
lines = split(buffer, vbcrlf) 'Split the buffer into lines
numlines = ubound(lines) + 1 'Determine the number of lines
redim firstcolumn(numlines - 1)
redim secondcolumn(numlines - 1)
'I've explained this part in my first post
for i = 0 to numlines - 1
tempbuf = split(lines(i), vbtab)
firstcolumn(i) = tempbuf(0)
secondcolumn(i) = tempbuf(1)
next i
'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]
-
Oct 30th, 2002, 02:52 PM
#5
Thread Starter
Addicted Member
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:
Dim array() as String
Open "C:\textfile.txt" For Input As #1
array = Split(Input(LOF(1), 1) , vbTab
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.
-
Oct 30th, 2002, 02:58 PM
#6
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|