# Double for loop with nested for loop dependant on outer

I would like to iterate over an array and then use the current value of the iterator as an argument to the next for loop. So far my code looks as follows but I can't get the syntax correct.

 \foreach \dest [count=\xi] in {2,...,4}
\foreach \source in {\xi-1,...,\xi+1}
\path (I-\source) edge (H-\dest); % do something with the source and dest values.


For example, in the outer loop the first value would be 2 and therefore the inner loop will go over {2-1,2,2+1}.

In the below image I have manually drawn in lines to illustrate the output, although it doesn't necessarily have to be of different colors. The idea is that each blue node in the first hidden layer is connected to exactly three of the input layer nodes, which is dependant on the index (Note I have missed out one green circle at the end by accident).

• Could you show us a practical use of this? That way we understand better what you want to achieve. – Alenanno Apr 18 '16 at 11:32

If you got trouble getting your data parsed the right way by the foreach-statement, you could do the following. Evaluate the values you need in the outer loop with evaluate and just pass them as the min/max values to the foreach.
\foreach [count=\i,