Reading from a text file into a jagged array
I found this question in an old question on your site, so I thought I could do it, but I think I was wrong :-)
old post was here
I am new to array core so did the following code
StreamReader rows = new StreamReader("c:\\practice.txt");
string line;
int i;
i=1;
while ((line = rows.ReadLine()) != null)
{
String[][]rows = new String [i][]; ;
rows = rows.ReadLine();
String[][] rows = new string[S.Length][];
i++;
}
for (int i; i < S.Length; i++)
{
row[i] = S[I].Split(',');
}
int totalCounter = 0, totalSum = 0;
// etc
foreach(string[] row in rows)
{
int m1 = int.Parse(row[3]);
totalCounter++;
totalSum += m1;
switch(row[2])
{
case "male":
maleCount++;
maleSum += m1;
break;
case "female":
femaleCount++;
femaleSum += m1;
break;
}
}
I know I was making serious mistakes, but in the east I was trying to have someone help me make the code to work.
You seem to be reading the lines twice, or maybe you messed up the lines and cells - this bit, in particular, looks really odd:
while ((line = rows.ReadLine()) != null)
{
String[][]rows = new String [i][]; ;
rows = rows.ReadLine();
String[][] rows = new string[S.Length][];
i++;
}
i.e. re-declaring strings, two calls ReadLine
per loop, etc. I suspect what you mean string.Split
? Anyway, either use File.ReadAllLines
or look at some of the options presented yesterday. If you are desperate to use arrays, the kernel might look something like this:
using System;
using System.IO;
static class Program
{
static void Main()
{
string[] lines = File.ReadAllLines("foo.txt");
string[][] grid = new string[lines.Length][];
for (int i = 0; i < lines.Length; i++)
{
grid[i] = lines[i].Split(',');
}
int totalCount = 0, maleCount = 0, femaleCount = 0,
m1Total = 0, m2Total = 0, m3Total = 0,
m1MaleTotal = 0, m1FemaleTotal = 0;
foreach (string[] line in grid)
{
totalCount++;
int m1 = int.Parse(line[3]),
m2 = int.Parse(line[4]),
m3 = int.Parse(line[5]);
m1Total += m1;
m2Total += m2;
m3Total += m3;
switch (line[1].Trim())
{
case "male":
maleCount++;
m1MaleTotal += m1;
break;
case "female":
femaleCount++;
m1FemaleTotal += m1;
break;
}
}
Console.WriteLine("Rows: " + totalCount);
Console.WriteLine("Total m1: " + m1Total);
Console.WriteLine("Average m1: " + ((double)m1Total)/totalCount);
Console.WriteLine("Male Average m1: " + ((double)m1MaleTotal) / maleCount);
Console.WriteLine("Female Average m1: " + ((double)m1FemaleTotal) / femaleCount);
}
}
Again - I can't stress how much you should be doing this with LINQ instead of a manual loop ...
source to share
First, make sure you are porting unmanaged resources like streams to using operators.
Personally, I like the LineReader class , which makes it easy to read lines of text from a file (or whatever, in fact).
Then I avoided using arrays unless you really need to. List<T>
is generally much more enjoyable to work with. Now if string.Split does what you want, you can easily have it List<String[]>
. Plus, you can probably do most of the work with LINQ:
var query = from line in new LineReader("c:\\practice.txt")
let parts = line.Split(',')
select new { Gender=parts[2], Amount=int.Parse(parts[3]) };
Taking multiple aggregates from a single data stream is tricky in "normal" LINQ (which is why Mark Gravel and I developed Push LINQ ). However, you can use a regular foreach statement:
int totalCounter = 0, totalSum = 0;
int maleCount = 0, maleSum = 0, femaleCount = 0, femaleSum = 0;
foreach (var row in query)
{
totalCounter++;
totalSum += row.Amount;
switch (row.Gender)
{
case "male":
maleCount++;
maleSum += Amount;
break;
case "female":
femaleCount++;
femaleSum += Amount;
break;
}
}
If you've grouped rows by gender, you could make it even easier, especially if you know that gender is always "male" or "female".
source to share