Pennsylvania United States
Member #1,340
April 6, 2003
2,581 Posts
Offline
I am almost done automating a system I am working on, I need help loading a CSV file into a 2 dimensional array... here is the code I have so far...
Dim intDrawHistory(1 To 10382, 1 To 3) As Integer
Dim intLoadArray As Integer 'Open statement Here For intLoadArray = 1 To 10382 Step 1 'load 1stnumber here =intDrawHistory (intLoadArray,1) 'load second number here =intDrawHistory (intLoadArray,2) 'load third number here =intDrawHistory (intLoadArray,3) Next intLoadArray
The red area is where I'm stuck... I still have to finish the blue placeholder yet to open the file P3.csv which just contains numbers
9,4,3
2,3,4
etc...
I'm trying to avoid the overhead and complexity of using ADO or OLE.
United States
Member #41,845
June 23, 2006
713 Posts
Offline
Quote: Originally posted by hypersoniq on May 12, 2007
I am almost done automating a system I am working on, I need help loading a CSV file into a 2 dimensional array... here is the code I have so far...
Dim intDrawHistory(1 To 10382, 1 To 3) As Integer
Dim intLoadArray As Integer 'Open statement Here For intLoadArray = 1 To 10382 Step 1 'load 1stnumber here =intDrawHistory (intLoadArray,1) 'load second number here =intDrawHistory (intLoadArray,2) 'load third number here =intDrawHistory (intLoadArray,3) Next intLoadArray
The red area is where I'm stuck... I still have to finish the blue placeholder yet to open the file P3.csv which just contains numbers
9,4,3
2,3,4
etc...
I'm trying to avoid the overhead and complexity of using ADO or OLE.
Any help greatly appreciated :-)
Hypersonic
First, I don't program in Vbasic. I'm stuck in the past with GWBasic.
I use basic to load data all the time, in a manner similiar to what you are doing. So a Question ? You outer loop loads the draw #, Why do you not use an inner loop to load the draw data? see below
for y=1 to 3 step 1 : rem i like to keep my variables simple
get data :rem with gwbasic it would be input #x,z with x being the file number(previously defined with an open statement) and z would be the data element
intdrawhistory(intloadarray,y)=data ( in this case z)
next y : rem completes inner loop loads 3 bits of data to the array
if you file contains other info such as date you will need to input that to a dummy variable to keep you data in correct order.
Switching between Fairfax, VA and Belgium Belgium
Member #19,286
July 29, 2005
2,254 Posts
Offline
Quote: Originally posted by hypersoniq on May 12, 2007
I am almost done automating a system I am working on, I need help loading a CSV file into a 2 dimensional array... here is the code I have so far...
Dim intDrawHistory(1 To 10382, 1 To 3) As Integer
Dim intLoadArray As Integer 'Open statement Here For intLoadArray = 1 To 10382 Step 1 'load 1stnumber here =intDrawHistory (intLoadArray,1) 'load second number here =intDrawHistory (intLoadArray,2) 'load third number here =intDrawHistory (intLoadArray,3) Next intLoadArray
The red area is where I'm stuck... I still have to finish the blue placeholder yet to open the file P3.csv which just contains numbers
9,4,3
2,3,4
etc...
I'm trying to avoid the overhead and complexity of using ADO or OLE.
Any help greatly appreciated :-)
Hi hypersoniq,
first, you should create a multidimensional array as should be done, meaning that the second dimension is the one that is flexible (i don't know the correct word in English, sorry...)
dim intDrawHistory () as Integer
Then you can redim it, using preserve, to the correct size
Redim intDrawHistory(3,AnyVariableHere) '--> 3 represents each digit of the number
Here is an example, where a textfile is saved as "example.csv" at "c:\example.csv" This is what is in the file, for example: 123 234 556 332
Dim iFile As Integer Dim intDrawHistory() As Integer Dim iDimension2 As Integer Dim sLine As String 'We will collect line per line
iFile = FreeFile iDimension2 = 0 'You use base = 1, so we'll set it to 0 for starters, and later on it will be set to 1
Pennsylvania United States
Member #1,340
April 6, 2003
2,581 Posts
Offline
Thanks for the input!
What I ended up doing was hard-coding the number of draws and the resulting array loading code is...
Dim intFileNumber As Integer Dim B1 As Integer Dim B2 As Integer Dim B3 As Integer intReadFileNumber = FreeFile Open "C:\P3.csv" For Input As intReadFileNumber Dim intLoadArray As Integer For intLoadArray = 1 To 10382 Step 1 Input #intReadFileNumber, B1 Input #intReadFileNumber, B2 Input #intReadFileNumber, B3 intDrawHistory(intLoadArray, 1) = B1 intDrawHistory(intLoadArray, 2) = B2 intDrawHistory(intLoadArray, 3) = B3 Next intLoadArray
I know I could have used shorter variable names, but I tend to forget what I did and it makes it easier on me to go back and have those descriptive names. (I trimmed out the comments also)