[Mono-list] Variable number of Lists depending on a CSV file

Ryan Boggs rmboggs at gmail.com
Sun Oct 9 13:19:35 EDT 2011


See below...

On Sun, Oct 9, 2011 at 9:53 AM, Paul F. Johnson
<paul at all-the-johnsons.co.uk> wrote:
> Hi,
>> > However, what I'd like to do is have a dynamic number of List objects
>> > depending on the number of columns.
>> This is the first thing that comes to mind, assuming every column in
>> the CSV file will be a double value.  However, I am not 100% sure if
>> this is the most efficient approach.
>> List<List<double>> rows = new List<List<double>>();
>> Here, you have a list within the list where the outer list would
>> represent each row in the CSV while the inner list would contain the
>> value of each column in the row of the CSV.
> Not quite sure how this would work. If my CSV has (say) 20 columns, how
> would I be able to read them in from a stream?
Since I am not sure how you are pulling the csv information in
exactly, I'll mock loading the list the best I can.

// Initialize the main list
List<List<double>> rows = new List<List<double>>();

// Indicate the expected number of columns in the csv
int colCount = 20;

// Loop through the csv
foreach(line in csv) {
    // Initialize a temp List<double> var to hold the column
    // values of the current line.
    List<double> row = new List<double>(colCount);

    // Loop through the columns of the current line.
    for(int i = 0; i < colCount; i++) {
        // Add the value of the current column to the temp list.
    // Add the row that was just pulled in from the csv file

After it's all loaded, you can retrieve whatever rows you want by
calling the indexes:
rows[14] // Will retrieve a List<double> all the columns in row 13 (zero index)
rows[19][2] // Will retrieve the double value of column 1 from row 18

I'm sure there are more ways to do it.  If anyone has a better
approach, please chime in. :)

> Thanks again
> --
> Vertraue mir, ich weiss, was ich mache...
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list

More information about the Mono-list mailing list