Perform Action Foreach Variable of a Specific INT in an Array C#

I need to know how to either: Check for a specific integer inside of an array and calculate how many times that integer appears in said array; or, run a method for each instance of a specific integer in said array.

I understand how to use the basic “foreach(int i in array)”, but I really can’t figure out how to define something other then i. I tried creating a new variable using a specific integer (for example “int value1 = 1;” and then calling that into the foreach statement by saying “foreach(int value1 in array)” but I didn’t get the results I needed.

I assume this is because it’s creating a local variable named value1 inside the foreach script, but I don’t really know.

int value1 = 1;
int[] array1;

void Start()
{
array1 = new int[5]
}

void CreatedMethod()
{
foreach(int value1 in array1)
{
invoke("CreatedMethod2",5)
}
}

void CreatedMethod2()
{
//Do something here
}

This is a very confusing question. So, I’ll just comment only on what I understand, or assume to understand. Maybe it will help clarify things.

First of all: It seems that you want to use value1, which you have assigned the value of 1, in the foreach loop. You can’t do that. Although it’s possible, is not good practice, and it won’t do what you want it to do. So to clarify: The local variable declared in the foreach loop is just a placeholder, so that you iterate through all the elements of the enumerator, and use the value of the placeholder, in each iteration, to do something with it.

So, in this particular case, in line 11 of your code, you are saying: Iterate through all the elements of array1, and each time round, put the value of the current element in the local variable named value1. This local variable value1, has the same name as the field value1 = 1, declared in line 1. Not a good practice to have variable with the same name, even in different scope.

Bow, having said that, Here’s what you can do to answer your question “Check for a specific integer inside of an array and calculate how many times that integer appears in said array”.

I assume that you want to check how many times value1 (that is equal to 1) appears in the array1.

int value1 = 1;
int[] array1;



void Start()
{
array1 = new int[5];  //or you could say in line 2, int[] array1 = new int[5]

}

int CountOccurrences(int valueToCheck) //set valueToCheck = value1 or anything else you want, when calling the method
{
int counter = 0;

foreach(int currentValue in array1)
{

if(currentValue = valueToCheck)
{
counter++;  //increase counter
}

}

return counter; 

}

To answer your question:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour 
{
	const int SEARCHED_INTEGER = 2;
	int[] _Integers;

	void Start () 
	{
		_Integers = new int[]{1,2,2,3,4};

		int numberOfSearchedInts = 0;
		foreach (int integer in _Integers)
		{
			if (integer == SEARCHED_INTEGER)
			{
				numberOfSearchedInts ++;
			}
		}

		Debug.Log ("We found " + numberOfSearchedInts + " occurrences of the number " + SEARCHED_INTEGER + " in the array" );
	}
}

Beading your question made me want to clear up a few things too though :slight_smile: :

You’re right. The “int value1 = 1;” you declare in the beginning of your class is indeed a “field” or a “class variable” and the “int value1” inside the foreach is just another variable with the same name but it’s a different one and only accessible inside the loop.

I also would like to make sure you really understand what happens in a foreach-loop, since even if what you’re trying to do with “value1” was possible scope- and access-wise, assigning the value “1” to it wouldn’t accomplish anything :slight_smile:

In a foreach, on each cycle, the variable you declare (value1) gets assigned the next value in the array you’re looping through. If you think about the meaning of “for each”, it really is misleading because you could think it’s something you can use to do something e.g. “for each (blueWhale in allWhalesArray)”, but foreach doesn’t provide any kind of filtering of content by itself. All it does is pull out values/references from the array and assign them conveniently for you in the variable you made.

Also remember that an int is a value type, so in your example if you assigned a different number to “value1” inside the foreach, the assigning doesn’t change the value inside the array, it only changes the value of “value1”.

If we are dealing with non-value types, it’s different

        GameObject[] _GameObjects = new GameObject[]{new GameObject(), new GameObject(), new GameObject()};
		foreach (GameObject gobj in _GameObjects)
		{
			// NOTE: GameObject is not a value type -> gobj is an actual reference to stuff inside the array
			// changing "gobj" really changes things inside the array.
			gobj.transform.position = Vector3.one;
		}