简体   繁体   中英

Changing a foreach loop to an if statement for reading a text file C#

Okay, so this is the code I have but I am trying to figure out how to change this foreach loop to an if statement. I've been trying to figure it out by myself but it doesn't seem to be working the way I want it to. So if anyone could help that would be much appreciated. Just as something extra, I am still a noob at C#. :)

// Used to make sure that the script can read the text file
    using (StreamReader sr = new StreamReader ("Maze.txt")) 
    {
        lineArray = File.ReadAllLines("Maze.txt");

        // Access one line at a time
        foreach (string line in lineArray)
        { 
            // Reset X axis for each line of the file
            int x = 0;

            // Access each character in the file
            foreach (char c in line)
            {
            string currentPosition = c.ToString();
            newFilePosition = Convert.ToInt32(currentPosition);

            if (newFilePosition == 1)
            {
                // Create a cube on the X axis
                NewCubePosition = new Vector3 (x, y, 0);
                Instantiate (g_mazeCubes, NewCubePosition, Quaternion.identity);
            }
            // Loop until X axis is done
            x++;
        }
    // Loop until Y axis is done
    y++;
}
}

If you are refering to transform a foreach to a for that test an condition. Making this transformation in code means you don't need to set x=0 and y=0 before and increment them inside foreach loop.

Using for you will do initialization, the condition, and the afterthought of x and y and iterate through array at once.

As many say you don't neet StreamReader . File.ReadAllLines opens and close the file for you .

lineArray = File.ReadAllLines("Maze.txt");

// Access one line at a time
for (y = 0; y < lineArray.Count(); y++)
{
    string line = lineArray[y];

    // Access each character in the file
    for (int x = 0 ; x < line.Count(); x++)
    {
        char c = line[x];
        string currentPosition = c.ToString();
        newFilePosition = Convert.ToInt32(currentPosition);

        if (newFilePosition == 1)
        {
            // Create a cube on the X axis
            NewCubePosition = new Vector3 (x, y, 0);
            Instantiate (g_mazeCubes, NewCubePosition, Quaternion.identity);
        }
    }
}

You can reduce the code with a bit of LINQ, but I don't think there's anything you can do to get rid of the inner loop.

var lineArray = File.ReadAllLines("Maze.txt");

for (var y = 0; y < lineArray.Length; y++)
{
    foreach (var c in lineArray[y].Select((value, i) => new { Character = value, Index = i })
                                  .Where(x => Convert.ToInt32(x.Character) == 1))
    {
        Instantiate(g_mazeCubes, new Vector3(c.Index, y, 0), Quaternion.identity);
    }
}

(You also don't need the StreamReader )

In my opinion if you try to reduce the the code further than this you'll obfuscate the intent with no benefit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM