I'm afraid I can't understand exactly what you are after...
But...
Would for each x element
with a condition searching 0 do the trick? (as it loops it picks up first instance)
Would for each x element
pick nth instance
Once you locate the 0, then you can change it with your 3,5
You might have to use a local variable to keep track of where it is, and maybe pass that back into another loop to pin point.
But, a function passing params it should neaten up and simplify nicely...
But, you lost me, as I can't visualize what you are doing.
I would use 0 height, 4 deep and push data onto x
x
0, 0,x,y
1, 0,x,y
2, 1,x,y
3, 0,x,y
etc
easier to search/locate/change