×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Reading CSV file into 2-dimensional array - How To?

Reading CSV file into 2-dimensional array - How To?

Reading CSV file into 2-dimensional array - How To?

(OP)
Colleagues,
Subject line says it.
I have a CSV file with, say, 10 columns and N rows. I need to read it into Nx10 array. All I could find was File.ReadAllLines(), but it can produce only 1-dimensional array, whereas I need 2-dimensional one.
Granted, I can take this one-dimensional array, read and parse each row and feed it into 2-dimensional one (takes some time and "sweat", but doable).
But I hope there's some other built-in .NET function that reads CSV into 2-dimensional array... is there?

Please advise!
TIA!

Regards,

Ilya

RE: Reading CSV file into 2-dimensional array - How To?

Hopefully someone will correct me if I am wrong, but…
You can declare a 2-dimension array (you do know first dimension, which is 10) and – after you find out the number of lines (records) in your CSV file – you can ReDim the second dimension.

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: Reading CSV file into 2-dimensional array - How To?

(OP)
Thank you, Andy but it ain't working:

If it's

CODE

Dim laData()
laData = File.ReadAllLines(lcPath2CSV)
ReDim Preserve laData(liRows, 10) 
then


If it's

CODE

Dim laData(0, 10) As String
laData = File.ReadAllLines(lcPath2CSV)
Dim liRows As Integer = laData.GetLength(0), liCols As Integer = 10, lsCSV As String = "", lcFld As String = ""
ReDim Preserve laData(liRows, 10) 

then



Any other suggestions?
TIA!

Regards,

Ilya

RE: Reading CSV file into 2-dimensional array - How To?

The error message is accurate - you are trying to change the number of dimensions of the array (from 1 to 2), and Redim cannot do that (and isn't what Andy was suggesting, he's really outlining a method for the solution you yourself outline in your original post). Also, you are trying to use Preserve - and if you use Preserve you can only resize of the last dimension of the array

>hope there's some other built-in .NET function that reads CSV into 2-dimensional array

Not that I am aware of

RE: Reading CSV file into 2-dimensional array - How To?

I was thinking more of the way like:

CODE

Dim intNoOfLines As Integer
Dim aryMyArray(10, 1) As String

intNoOfLines = 20   'Get the number of records from your CSV file

ReDim aryMyArray(10, intNoOfLines)
'Populate your array here 

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: Reading CSV file into 2-dimensional array - How To?

(OP)
ReDim aryMyArray(10, intNoOfLines)
Right!
Years of working with tables misled me to believe that I can add a record (row) to an array, whereas I can add only a field (column). I forgot about this shortcoming of VB.
Thank you for reminding me!
Other than that (which is mea culpa!) - great post!

Regards,

Ilya

RE: Reading CSV file into 2-dimensional array - How To?

"I can add a record (row) to an array" - Yes, you can.
If you have an array aryMyArray(10, 20), you can add 'a record (row)' by:

ReDim Preserve aryMyArray(10, 21)

What you actually doing is - making a copy of the original array with an additional 'empty piece', but you can only do that with the last dimension

You cannot do: ReDim Preserve aryMyArray(15, 20)

Just realized - that all depends on what you consider a 'row' and a 'column' in an array... smile

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

RE: Reading CSV file into 2-dimensional array - How To?

If you are not comfortable with REDIM then you can do that as I'm used to do it in Fortran

CODE

'1) read the file 1.time for counting the lines, and setting intNoOfLines

'2) declare the array
Dim MyArray(10, intNoOfLines)

'3) Read the CSV file 2.time and populate MyArray 

RE: Reading CSV file into 2-dimensional array - How To?

>this shortcoming of VB

I'd point out that the limitation is actually the underlying datatype that VB uses, which is an Ole SafeArray. So blame Ole, not VB ...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close