Simple nested loop without code, but trees: if(i != j) problem

Abstract example to illustrate my problem/question:

one list of points (in a grid)
one list of meshes (in a grid)

For each point check all meshes in the list for their closest point, ignoring the mesh that has the same index as the current point.

The second part of the sentence is the problem - how to ignore the current index in a different list?

In code it is simple:

for(int i = 0; i < points.Count; i++) {
	for(int j = 0; j < meshes.Count; j++) {
		if(i != j) {
			// do some work
		}
	}
}

but how to do this without code? The setup described here is quite abstract, but it is an everyday task in programming to loop lists while ignoring ‘itself’ - all kinds of neighborhood problems require this functionality…

Why without code - some examples i want to show students, but without requiring them to code…

Hello @atair,

It’s quite straightforward to achieved this without scripting:

Note though that, if the points are ordered differently than the meshes, meaning that each mesh doesn’t correspond to its closest point in the first place, the ignored indices won’t be those of closest points on closest meshes.
However, reading your description above that wasn’t what you asked for either.
As a teacher, you should have known better and provided a file.

I hope this helps, though.

codeless_nested_loop_01.gh (12.5 KB)

1 Like

You are helping them to become unemployed sooner or later eh? he, he.

Jokes apart using a bool Array (say: bool[,] visited = new bool[list1.Count, list2.Count]) … or a custom Class is the orthodox way to go. If fact a Class is far superior since allows you to use LINQ queries that extend by far the filtering capabilities (other than the “is that you” [i != j] ?) .

For instance imagine that we have N polylines that either are connected (some node in this poly with some node on that poly) or they are apart. How to close the gaps? (within a search Interval [tol, searchRange]) How to monitor what’s happened in the double loop required? That sort of stuff is easily addressed via a Class (in this case stuff that monitors Pairs).

1 Like

Thanks! so simple!
next time with file :wink: