Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
568
US
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
 
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
 
Thank you, Andy but it ain't working:

If it's
Code:
Dim laData()
laData = File.ReadAllLines(lcPath2CSV)
ReDim Preserve laData(liRows, 10)
then
2022_09_06_13_52_CSV2ArrayErr_o3c3rp.jpg


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

2022_09_06_13_54_CSV2ArrErr_itpqic.jpg


Any other suggestions?
TIA!

Regards,

Ilya
 
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

 
I was thinking more of the way like:

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

intNoOfLines = 20   [green]'Get the number of records from your CSV file[/green]

ReDim aryMyArray(10, intNoOfLines)[green]
'Populate your array here[/green]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
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
 
I can add a record (row) to an array" - Yes, you can.
If you have an array [tt]aryMyArray(10, [red]20[/red])[/tt], you can add 'a record (row)' by:

[tt]ReDim Preserve aryMyArray(10, [red]21[/red])[/tt]

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: [tt]ReDim Preserve aryMyArray([highlight #FCE94F]15[/highlight], 20)[/tt]

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

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
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
 
>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 ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top