Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Komag
Roku Guru

Nested FOR EACH loops behavior

I ran into behavior where if I'm doing a FOR EACH on a list, and then nesting in another FOR EACH on the same list, the internal/nested FOR EACH apparently moves the "pointer" or position to the end of the list for BOTH loops, such that the larger FOR EACH never does anything but the first element!

FUNCTION findPathAdjs(rm, pathList) ' trig by prepPathLists(1)
'FOR EACH vertex IN pathList ' Can't use FOR EACH here since we need to look through this same list below and that moves the "pointer" through to the end of of the list!
FOR v = 0 TO pathList.Count() -1
vertex = pathList[v]
y = vertex.y
x = vertex.x
? "findPathAdjs() rm" rm ", y" y ", x" x
adj = [] ' List of adjacent non-blocked squares
adjCand = [ [y-1, x], [y, x+1], [y+1, x], [y, x-1] ] ' N, E, S, W
FOR adjDir = 0 TO 3
coords = adjCand[adjDir]
y2 = coords[ 0]
x2 = coords[ 1]
IF NOT offGridCheck(y2, x2)
adjVertex = y2.ToStr() + "x" + x2.ToStr()
? "findPathAdjs() checking for " adjVertex " in rm" rm " from y" y ", x" x
FOR EACH vertexB IN pathList ' I can use FOR EACH here without issue
IF vertexB.vtxNm = adjVertex ' If the adjacent vertex name is on the non-blocked square list for the room...
adj.Push(adjVertex) ' Add it to the short list for adj squares
EXIT FOR ' If we found a match, no need to look further
END IF
END FOR
END IF
END FOR
? "findPathAdjs() rm" rm ", y" y ", x" x ", adj.Count()" adj.Count()
vertex.adj = adj ' assign the built array to the vertex as key "adj"
END FOR
RETURN pathList
END FUNCTION


My list is pathList, and when I tried using FOR EACH in that top line it would only do one element. Now that I'm using actual numbers, it gets them all.

Is this normal to programming and something that just reveals my general ignorance, or is this some sort of shortcoming/weakness of BrightScript?
0 Kudos
1 REPLY 1
destruk
Streaming Star

Re: Nested FOR EACH loops behavior

It has been discussed before, and is normal.  Your solution is the 'fix'.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.