0

So I'm working on a project in Unity in C# and I have to get information from a csv file. I made a test file to develop the code, but I have a little issue.

The test csv file contains (3 rows, each have 5 columns):

5,9,12,53,67
8,24,33,49,71
10,16,28,64,88

I'm using the code below to read in to a jagged array. When I print to the console (with the debug.log function) the lengths of each row, I get the correct answer. But if I use a for loop, it only gets the first row's length, but not the other two. How is this possible? As far as I know for loop should work with it perfectly. No errors.

Any ideas?

Thanks for your help!

The code:

StreamReader test = new StreamReader("D:/Unity/Projects/.../Assets/Resurces/test.csv");
var testLines = new List<string[]>();
int row = 0;

while (!test.EndOfStream)
{
    string[] Line = test.ReadLine().Split(',');
    testLines.Add(Line);
    row++;
}

var data = testLines.ToArray();

Debug.Log(data.Length);
Debug.Log(data[0].Length);
Debug.Log(data[1].Length);
Debug.Log(data[2].Length);

for (int i=0; i<data.Length; i++)
{
    Debug.Log("i: " + i);
    Debug.Log(data[i].Length);
}

What I get on the console:

3
5
5
5
i: 0
5
i: 1
i: 2
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • The results are as expected, data.length is the count of lines, data[n].length is the number of elements per line. You would want 2 loops 1 for each line, and 1 for each item on the line – BugFinder Feb 09 '20 at 13:02
  • @BugFinder you would expect the code above to print i: 1, 5, i: 2, 5 (on four lines) – iakobski Feb 09 '20 at 13:21
  • @MárkBurka I've just run your code and it doesn't show the problem you describe. There must be something else you've not shown us - are you *sure* the code above is the code that produces that output? – iakobski Feb 09 '20 at 13:22
  • @BugFinder no, I would expect that it would give me the same with the for loop like I did manually: 3, 5, 5, 5, i: 0, 5, i: 1, 5, i: 2, 5 – Márk Burka Feb 09 '20 at 14:11
  • @iakobski No, I copy pasted my code here, so everything is the same. Did you use unity as well, in the start, in MonoBehaviour? Maybe unity does something weird? – Márk Burka Feb 09 '20 at 14:16
  • That doesn't make any sense. The Debug.Log line should print *something* even a blank line, or throw an exception! – iakobski Feb 09 '20 at 15:16
  • @iakobski Yeah, I know. That's why I had no clue why is it working like this and come here for help. – Márk Burka Feb 09 '20 at 19:09
  • then its not entirely clear what your issue is, as it looks OK to me – BugFinder Feb 10 '20 at 08:11

3 Answers3

0

I had unity open and your code looked like it should work, so I tested it in a Start function of an otherwise empty monobehaviour and got the expected result.

EDIT: You very likely have "Collapse" turned on in your console, it's a feature not a bug, you can turn it on to prevent something spamming the console every update, its hiding the later loops, because the code and the result are both identical to the first loop.

Adam
  • 39
  • 9
  • Restarted unity, made a new script file and it still giving me the same results. In notepad++ every line has de CRLF line ending. – Márk Burka Feb 09 '20 at 19:27
  • I made an entirely new project, there is nothing else in it just the test.csv and a new script, that has the code I have posted. Corrected the path to the file, but the results are the same. I have no clue what is wrong. It should work fine. – Márk Burka Feb 09 '20 at 19:29
  • Oh, I know what it is! The label of your console tab, right below it there are buttons labelled "Clear" "Collapse" "Clear On Build" etc.. Im betting you have "Collapse" highlighted, right? When you turn it off, you will see that Unity has been hiding the later messages, because they are exactly the same as previous messages. – Adam Feb 09 '20 at 19:46
  • Yep... that was it. This... yeah. A few swear words just came out, that how stupid I am. Is this automagically on in every new project? Because I don't remember turning it on. Thank you very much! – Márk Burka Feb 09 '20 at 22:50
  • Don't be too hard on yourself, it's the sort of thing that catches everyone once if they use unity long enough. ;) – Adam Feb 10 '20 at 01:27
0

So apparently there is a button in unity called Collapse at the console. If it's on, unity will hide the messages if they are the same. Never used it, I don't remember to turning it on, but looks like it did what it suppose to do. Thanks everyone the help, especially to @Adam, who pointed me to this button!

-1

I debugged your code snippet in a sample unity project and i see desired results. Please verify once again. An alternative would be to try foreach.

enter image description here

  • I made an entirely new project, there is nothing else in it just the test.csv and a new script, that has the code I have posted. Corrected the path to the file, but the results are the same. I have no clue what is wrong. It should work fine. – Márk Burka Feb 09 '20 at 19:30
  • try the below. Not sure if this works for you! foreach (var item in data) { Debug.Log(item.Length); } – Rajesh Nagendra Feb 10 '20 at 06:07