views:

971

answers:

5

Hi,

I got a table (in file) which I split into blocks by spaces.

I need structure like this:

-----------------------------
|21|22|23|33|3323|
|32|32|
|434433|545454|5454|
------------------------------

It's more like each row is its own table. How should I do this?

I tried List<List<string>> matrix = new List<List<string>>(); but I can't seem to find a way to work with it.

EDIT - can someone tell me what's wrong with this code???? Matrix[0][0] is same as matrix [1][0].. it seems that same row is added to matrix all the time, but I clear it ...

static ArrayList ReadFromFile(string filename)
    StreamReader SR;
    string S;
    string[] S_split;

    SR = File.OpenText(filename);
    S = SR.ReadLine();

    ArrayList myItems = new ArrayList();

    List<List<string>> matrix = new List<List<string>>();
    List<string> row = new List<string>();

    while (S != null)
    {
        row.Clear();
        S_split = S.Split(' ');
        for (int i = 1; i < S_split.GetLength(0); i++)
        {
            row.Add(S_split[i]);
            matrix.Add(row);
        }              

        S = SR.ReadLine();
    }
    Console.WriteLine(matrix[1][1]);
    SR.Close();
    return myItems;
}
+1  A: 

You should be able to work with it as you'd expect to deal with a list within a list.

matrix.Add(new List<string>);
matrix[0].Add("a string");
RSlaughter
+1  A: 
List<List<String>> matrix = new List<List<String>>();

foreach (String line in file)
{
   String[] values = line.Split(new Char[] { ' ' });

   matrix.Add(new List<String>(values));
}

Just iterate through your file and for every line do the following.

  1. Generate a new List
  2. Fill it with the data for the line
  3. Add the list for the current line to your list for the complete file

Note that foreach (String line in file) is just pseudo code. Further you can merge the to lines in the body to a single line.

matrix.Add(new List<String>(line.Split(new Char[] { ' ' })));
Daniel Brückner
+2  A: 

Not sure if I understand this correctly.

        List<List<int>> table = new List<List<int>>();
        List<int> row = new List<int>();
        row.Add(21);
        row.Add(22);
        row.Add(23);
        row.Add(33); // and so on
        table.Add(row);

        row = new List<int>();
        row.Add(1001);
        row.Add(1002);
        table.Add(row);

        MessageBox.Show(table[0][3].ToString());

The program should show a message box with text "33".

m3rLinEz
ok and how do I get number of "rows" in a "table" to test if I'm right?
Skuta
table.Count() returns the number of rows.
Daniel Brückner
you can use table.Count() as @danbruc suggests :) for individual row, use table[0].Count()
m3rLinEz
+1  A: 

You're describing a jagged array. I'm not exactly sure if a List won't be overkill? If you just have to import the data from the file and than use it, a jagged array should be simple enough.

Example:

int[][] jaggedArray = new int[][] 
{
    new int[] {21, 22, 23, 33, 3323},
    new int[] {32, 32},
    new int[] {434433, 545454, 5454}
};

you can also buildup the jagged array in a loop while processing your file, like this:

int[][] result = new int[numberOfLines][];
for (int currentLine = 0; currentLine < numberOfLines; currentLine++)
{
    String line = fileStream.ReadLine();
    int[] values = SplitAndConvertLine(line);
    result[currentLine] = values;
}
Davy Landman
The problem using jagged arrays is that you have to know the size when creating it. You have the numberOfLines variable in your code but you often don't know in advance. So you build up a list and return a array if you finished. This adds more overhead then just returning the list.
Daniel Brückner
And if you will have to manipulate the data later, you will probably create a list again, because you need to add or remove something. So I absolutly prefer list because of the gained flexibility. And under the hood it is just a array with some smart resizing - not that much overhead.
Daniel Brückner
I'm offering it as a alternative, and already noted that it would not be wise to use them in case of changing data. Depending on the size of the data, it might be wise to know certain properties in advance, so the List can reserve a large capacity in advance.
Davy Landman
A: 

If you are trying to read from the file then try something like the following (Note the following code has not been run through a compiler)

List<string[]> matrix = new List<string[]>();
while(!instream.EndOfStream)
{
    var values = instream.ReadLine().Split(new[] {' '});
    matrix.Add(values);
}
SDX2000