0

I have a multidimensional array.

The contents look like this in the debugger.

The array is setup as

String[,] s = new String[6,4]

.

A B Yes C
A B Yes C
A B No  C
A B Yes C
A B Yes C
A B Yes C
A B No  C
A B Yes C

I basically need to know which row's say No but I am having a tough time parsing the array. Any help would be appreciated.

4
  • If you need 'parsing' then a multidom array is not the best datastructure. Why is it not a List<MyData> ? Commented Feb 16, 2012 at 14:05
  • possible duplicate of how do you loop through a multidimensional array? Commented Feb 16, 2012 at 14:07
  • This really is already covered by the answers you got yesterday. Commented Feb 16, 2012 at 14:08
  • 1
    Your sample data shows 4 rows and 4 columns, so it's impossible to tell which array dimension represents the rows and which represents the columns. Commented Feb 16, 2012 at 14:13

3 Answers 3

1

so?

        [TestMethod]
        public void test()
        {
            var text = new String[6, 4]
                               {
                                   {"A", "B", "C", "Yes"},
                                   {"A", "B", "C", "Yes"},
                                   {"A", "B", "C", "Yes"},
                                   {"A", "B", "C", "Yes"},
                                   {"A", "B", "Not", "C"},
                                   {"A", "B", "C", "Yes"}
                               };
            var rowWithNot = new List<int>();

            for (int row = 0; row < 6; row++)
                for (int col = 0; col < 4; col++)
                    if (text[row, col].Contains("Not"))
                    {
                        rowWithNot.Add(row);
                        break;
                    }

            foreach (var row in rowWithNot)
            {
                for (int col = 0; col < 4; col++)
                {
                    Console.WriteLine(text[row, col]);
                }

            }
         }
Sign up to request clarification or add additional context in comments.

Comments

1
var rowIndices = Enumerable.Range(0, s.GetLength(0)).Where(i => s[i, 2] == "No");

2 Comments

This looks really elegant but is rowIndicies an array of Indexs?
It is an enumerable sequence. Add a ".ToList()" at the end to get a list.
0

you can do it using something like this :

IEnumurable<int> GetRowNumbersThatSayNo(string[,] values)
{
for(int i=0;i<values.Length;i++)
if(values[i,2]=="No") yield return i;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.