locked
csv to array??

    Question

  • I am new to .net programming so please bear with me if I seem like I don't know what I am talking about, because I probably don't.  I used to play around with vb6 but never did any thing more than a few exercises from a Learn vb6 book.

    I want to read a csv file into something.  I was thinking an array, but after searching the forums, I see TextFieldParser, Collections, Generic Collections, DataGridView, etc...  I really don't know what all of this stuff is and would really appreciate some help.

    What I need to do is read in data from a csv file.  (I started by using the StreamReader and am able to read the file line by line). The file is in the format of <Record Number>,<Date> <Time>,<0,1 or Null>,<0,1 or Null>,<0,1 or Null>,<0,1 or Null>.

    What I need to do is compare the <0,1 or Null> Fields with the others and depending on the data in those fields, do a time comparion and create a histogram of each state. 

    Another thing I need to do (maybe not need to, but would make it easier (I think)) is if the value in the field is Null, put in a 0 or 1 depending upon what the previous record was.  If I could get all the data into an array (if I use arrays), doing this seems simple to me.

    Thank you,

    Keith

    Thursday, June 29, 2006 5:56 PM

Answers

  • Got it.

    Had to change arrInputData(i,j) = recsplit(j) to arrInputData(i,j) = NEW recsplit(j).

    I also changed my Dim arrInputData to Dim arrInputData(lengthoffile,5) As String.

    How can I Dim my array as a global variable without knowing the length of the array?

    Thanks

    Thursday, June 29, 2006 7:43 PM

All replies

  • You can read line by line your file, and you can carry each record into an string array. Positions of the array are the fields of the record.

    How?  After you read each record, you can use the Split(..) function in order to create an array based on an specific separator.


    Dim delim as String = ","  'define a delimiter
    Dim record as String     ' record storage
    Dim recsplit() as string    ' splited record storage

    'open the file
    ....

    'while not end of the file

       record = stringReader.ReadLine() 'read a record 

       recsplit() = Split(record, delim) 'split the record 

       'make your calculations.....
       'each position of recsplit can be accesed normally

     

    Let me know if this info helps.

    Thursday, June 29, 2006 6:45 PM
  • I've got that far using that method.  I want to take that one step further and make the recsplit a 2d array basically containing my csv file.

    Then, I should be able to:

    Dim arrInputData(,) As String            'Is this the proper way to dimension an unknown length array of strings???

    For i = 1 to LengthOfFile

    readline

    recsplit =

    For j = 1 to 5

    arrInputData(i,j) = recsplit(j)

    Next

    Next

    When I do that, I get a NullReferenceException on the arrInputData(i,j) = recsplit(j) line.  The troubleshooting tips are:

    "Use the new keyword to create an object instance"

    "Check to see if the object is null before calling the method."

    I don't know what either of those two tips mean to do.

    Thanks,

    Thursday, June 29, 2006 7:19 PM
  • Got it.

    Had to change arrInputData(i,j) = recsplit(j) to arrInputData(i,j) = NEW recsplit(j).

    I also changed my Dim arrInputData to Dim arrInputData(lengthoffile,5) As String.

    How can I Dim my array as a global variable without knowing the length of the array?

    Thanks

    Thursday, June 29, 2006 7:43 PM
  • ok,

    i made a snippet for you. this snippet read the entire file to memory in an arrayList. This is a good aproximation to the solution you need.

    Each element of the arrayList is an array of information. Each record in the file must be delimited, and each field in the record must be comma delimited too.

          Dim rDelim As String = vbCrLf 'record delimiter

          Dim fDelim As String = "," 'field delimiter

          Dim sr As StreamReader = File.OpenText("myCSVfile.csv")  

          Dim cvsRecord As String  

          Dim memFile As System.Collections.ArrayList

          memFile= New System.Collections.ArrayList

               

          cvsRecord = sr.ReadLine()

          Do While cvsRecord <> Nothing

              ' load each record to the array of records
                         memFile.Add(cvsRecord.Split(
    ","))    

              cvsRecord = sr.ReadLine()

          Loop   

     

         ' this fragment is for reading the fields of the file

          For Each record As Object In memFile

                For Each field As Object In record

                      ''''' your work here

               Next

          Next

     

    Thursday, June 29, 2006 8:01 PM
  • You can use an ArrayList or List(Of) object instead of an array.  This way you don't need to know the final length.  You just keep adding items as necessary.

    You might also want to check the thread:
    http://forums.microsoft.com/MSDN/showpost.aspx?postid=515398&siteid=1

    There are a couple examples of loading a CSV into a DataTable.  Just replace the addtion of new DataRows to a DataTable with code to add items to a list.

    Thursday, June 29, 2006 8:41 PM
    Moderator