user1925442
user1925442

Reputation: 45

How to skip first element in each line of matrix?

I have next matrix:

1 4 2 1 3 5
2 3 1 2 4 5
3 4 5 3 2 1
4 5 1 3 4 2
5 3 2 4 5 1

and I am writing it from text file to 2d array starting from second line in file in this cycle:

foreach (var line in File.ReadAllLines(@"input_5_5.txt", Encoding.GetEncoding(1250)).Skip(1))
            {
                    foreach (var col in line.Trim().Split(' '))
                    {
                        result[i, j] = int.Parse(col.Trim());
                        j++;
                    }
                    i++;
            }

I need to skip first element in each line of matrix and write to array result from second element in every line because first element in row of matrix is only number of row.

Upvotes: 0

Views: 1775

Answers (4)

Pieter21
Pieter21

Reputation: 1845

My exercise for this question, though some Linqs can still be shortcircuited, this may be more clear:

    [TestMethod]
    public void LinqArraySkipFirstColumnAndLine()
    {
        var inputString = @"SkipThisLine
                            1 4 2 1 3 5
                            2 3 1 2 4 5 
                            3 4 5 3 2 1
                            4 5 1 3 4 2
                            5 3 2 4 5 1";

        char[] lineSeparator = new char[] { '\n' };
        char[] itemSeparator = new char[] { ' ' };

        var lines = inputString.Split(lineSeparator).Skip(1);
        Assert.AreEqual(5, lines.Count(), "Expect 5 rows");

        List<List<int>> matrix = new List<List<int>>();

        lines.ToList().ForEach(line => {
            int dummy = 0;
            var items = line.Trim()
                            .Split(itemSeparator,
                                   StringSplitOptions.RemoveEmptyEntries)
                            .Skip(1);
            Assert.AreEqual(5, items.Count(), "Expect 5 items each row");

            var row = items.Where(c => int.TryParse(c, out dummy))
                           .Select(w => dummy).ToList();
            matrix.Add(row);
        });

        var elements = from row in matrix
                       from cell in row
                       select cell;
        Assert.AreEqual(25, elements.Count(), "Expect 25 elements after flattening matrix");
    }

Upvotes: 0

dougajmcdonald
dougajmcdonald

Reputation: 20057

There are many variations on how to do this, but you want to start j at 1 instead of 0. that was you're missing element 0, which I believe if what you're after, so:

foreach (var line in File.ReadAllLines(@"input_5_5.txt", Encoding.GetEncoding(1250)))
{
    int j = 1;
    foreach (var col in line.Trim().Split(' '))
    {
        result[i, j] = int.Parse(col.Trim());
        j++;
    }
    i++;
}

Upvotes: 0

Whoami
Whoami

Reputation: 334

You should instead use (if you need to know which line/col). Col is initialized with 1 since you don't need first column.

string[] fileLines = System.IO.File.ReadAllLines(@"input_5_5.txt", Encoding.GetEncoding(1250));
for(int line = 0; line < fileLines.Length; line++)
{
    string[] splittedLines = fileLines[line].Trim().Split(' ');
    for(int col = 1; col < splittedLines.Length; col++)
    {
        // do whatever you want here
    }
}

Upvotes: 1

Vikas Gupta
Vikas Gupta

Reputation: 4465

I'd advise to simply skip foreach and use a regular for loop. After trimming and splitting the line by space(s), simply skip the 1st element by initializing the index to 1 instead of 0.

var cols = line.Trim().Split(' '); //use string split option to remove empty entries for robustness

for (int j = 1; j < cols.Length; j++) //note the j is initialized with 1, instead of 0

Note - All this assumes that either the data is known to be well formed, or you do appropriate bounds and error checks etc.

Upvotes: 1

Related Questions