IvanHowarth
asked on
Loading the contents of a csv file into a 2D array - The array x value is being assigned to each array y value
Lets say I have a csv file containing:
Jim Jims Address Jims Tel
John JohnsAddress JohnsTel
Load this into an array and I get this:
Jim0 Jims Address0 Jims Tel0
John1 JohnsAddress1 JohnsTel1 etc
Two questions: 1) How can I avoid the appended values? 2) see code below at '?????'
The code used:
Dim DB(,) As String
Try
FileOpen(1, SourceFilePathAndName, OpenMode.Input)
TotalNumberOfLines = 4 ' ????????????? How can I find this if I don't know it in advance?
TotalNumberOfColumns = 3
DB = New String(TotalNumberOfLines - 1, TotalNumberOfColumns - 1) {}
For i As Integer = 0 To TotalNumberOfLines - 1
For y As Integer = 0 To TotalNumberOfColumns - 1
Input(1, DB(i, y))
Next y
Next i
Catch etc...
Finally
FileClose(1)
End Try
Jim Jims Address Jims Tel
John JohnsAddress JohnsTel
Load this into an array and I get this:
Jim0 Jims Address0 Jims Tel0
John1 JohnsAddress1 JohnsTel1 etc
Two questions: 1) How can I avoid the appended values? 2) see code below at '?????'
The code used:
Dim DB(,) As String
Try
FileOpen(1, SourceFilePathAndName, OpenMode.Input)
TotalNumberOfLines = 4 ' ????????????? How can I find this if I don't know it in advance?
TotalNumberOfColumns = 3
DB = New String(TotalNumberOfLines - 1, TotalNumberOfColumns - 1) {}
For i As Integer = 0 To TotalNumberOfLines - 1
For y As Integer = 0 To TotalNumberOfColumns - 1
Input(1, DB(i, y))
Next y
Next i
Catch etc...
Finally
FileClose(1)
End Try
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
Glad I could help. :=)
sRecords = sBuffer.Split(vbCrLf)
for i = 0 to sRecords.Length
if sRecord(i) <> "" then
...
Then replace every comma which is outside of quotes with a chr(2) or some other character that wont occur in data
Then split the record into fields
sfield = split(sRecord(i), chr(2))
and process each field in a loop
If you really need it in a 2D loop you can parse it into a new array