Solved

Loading the contents of a csv file into a 2D array - The array x value is being assigned to each array y value

Posted on 2006-07-06
4
259 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:IvanHowarth
  • 2
4 Comments
 
LVL 8

Expert Comment

by:plq
ID: 17050243
Using Input seems wrong. Read the whole file into a buffer and then use the split function to split it into records. As long as the file is <10MB you should be fine.

    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
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17050900
Hi IvanHowarth;

I re-wrote your code to do it in a VB .Net way. Also this code can expand columns and rows without any code re-write. From your sample data it looked as the file is Tab delimited if it delimited by a different character then change in one line in the code, it is commented.

To your two questions
1) How can I avoid the appended values?

I do not see where in your code it is appending a value to the field. You read a line from the file and write it into the array.

2) see code below at '?????'

Answer is in the sample I have given below.

        Dim SourceFilePathAndName As String = "C:\Temp\cvs.txt"
        ' VB .Net way to open and read a file
        Dim sr As New System.IO.StreamReader(SourceFilePathAndName)
        ' Read the whole file into memory and split on the CrLf into an array
        Dim cvsFile() As String = sr.ReadToEnd().Split(CChar(ControlChars.CrLf))
        ' Close the input file
        sr.Close()
        Dim TotalNumberOfLines As Integer = cvsFile.Length - 1
        Dim TotalNumberOfColumns As Integer = 3
        ' Create a multi dim array
        Dim DB(TotalNumberOfLines, TotalNumberOfColumns - 1) As String

        ' Loop through the array of input lines
        For i As Integer = 0 To TotalNumberOfLines
            ' Split on tab deliniter or change to meet the correct delimiter being used
            ' Each field is in its own element of the array
            Dim fields() As String = cvsFile(i).Split(CChar(ControlChars.Tab))
            ' Place elements into the final array
            For y As Integer = 0 To fields.Length - 1
                DB(i, y) = fields(y).Trim()
            Next
        Next


Fernando
0
 

Author Comment

by:IvanHowarth
ID: 17064431
Thanks!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17064772
Glad I could help. :=)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
scanning dentists xray (the small ones) 3 53
Help with Syntax 9 36
Prevent call a sub/function several times when data bind to gridview 21 29
VB.NET Repostiory Pattern 7 35
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question