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!
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?
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?
If it's
CODE
If it's
CODE
then
Any other suggestions?
TIA!
Regards,
Ilya
RE: Reading CSV file into 2-dimensional array - How To?
>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?
https://docs.microsoft.com/en-us/dotnet/visual-bas...
RE: Reading CSV file into 2-dimensional array - How To?
CODE
---- Andy
"Hmm...they have the internet on computers now"--Homer Simpson
RE: Reading CSV file into 2-dimensional array - How To?
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?
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...
---- Andy
"Hmm...they have the internet on computers now"--Homer Simpson
RE: Reading CSV file into 2-dimensional array - How To?
CODE
RE: Reading CSV file into 2-dimensional array - How To?
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 ...