1

How can I modify my method to check only indexes that are equal zero while modulo 17? I would like to get from that indexes all items that are equal also zero.

My List<byte[]> listOfArrays is storing arrays with values 0,1

This is my method:

public List<int> Funct(List<byte[]> listOfArrays)
        {
            List<int> pixelsList = new List<int>();
            foreach (byte[] t in listOfArrays)
            {
                //count how many items with value 0
                var pixelsInArray = t.Count(n => n == 0);

                var firstElement = Array.FindIndex(t, i => i == 0);
                var lastElement = Array.FindLastIndex(t, i => i == 0);

                pixelsList.Add(pixelsInArray);
            }
            return pixelsList;
        }

In that method I search in all indexes.

I would appreciate your help.
PS. Please check my grammar if need to correct.

deadfish
  • 11,064
  • 11
  • 79
  • 125

1 Answers1

1

this should do the trick if i understand properly:

  public List<int> Funct(List<byte[]> listOfArrays)
  {
     List<int> pixelsList = new List<int>();
     foreach (byte[] t in listOfArrays)
     {
        int counter = 0;
        t.ForEachWithIndex((value, index) =>
                              {
                                 if (value == 0 && index % 17 == 0)
                                 {
                                  // your values
                                  counter++;                                      
                                 }
                              }); 

       pixelsList.Add(counter);
     }

     return pixelsList;
  }

Extension found in: How do you get the index of the current iteration of a foreach loop?

namespace MyExtensions
{
   public static class ForEachExtensions
   {
      public static void ForEachWithIndex<T>(this IEnumerable<T> enumerable, Action<T, int> handler)
      {
         int idx = 0;
         foreach (T item in enumerable)
            handler(item, idx++);
      }
   }
}
Community
  • 1
  • 1
Kamil Lach
  • 4,179
  • 2
  • 17
  • 20
  • yup, byt do not add instantly to list but just count :) but this code is also helpful – deadfish Oct 29 '11 at 10:55
  • Ok, i fix it :) Is that what you mean ? – Kamil Lach Oct 29 '11 at 10:59
  • yes it supposed to be however it doesn't work for me. `ForEachWithIndex` isn't recognised. I am trying find out why.. – deadfish Oct 29 '11 at 11:04
  • I put those class in same namespaces, you sure that you add "using" to ForEachExtensions (if it's in another namespace) ? – Kamil Lach Oct 29 '11 at 11:07
  • It's the same namespace, it says `'System.Array' does not contain a definition for 'ForEachWithIndex' and no extension method 'ForEachWithIndex' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)` – deadfish Oct 29 '11 at 11:14
  • Wait :D Should i implement that classForEachWithIndex first? – deadfish Oct 29 '11 at 11:17
  • 1
    yeap :) you shuld put it somewhere in your project, it's not in .Net framework by default – Kamil Lach Oct 29 '11 at 11:18
  • I've just add namespace "MyExtensions" to those class, and in class where you using it you shuld put: using MyExtensions; on the top – Kamil Lach Oct 29 '11 at 11:21