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: 
btpoole
Channel Surfer

Untangle Loops

Having some difficulty with some loops and wanted to see if anybody could shed some lite on it. I am looping thru one array (controlarray) comparing that to an element of a second array (myarray), if they match, I want to save some of the elements to a final array, but only the first arrays of the match. Here's what I have so far, by the way the controlarray has a count of 35 and the myarray has a count of 145 so I know both are populated.


for i= 0 to controlarray.count()
'?"VALUE OF I. . ."i
'?"VALUE OF CONTROLARRAY. . ."controlarray[i]
if controlarray[i]<> invalid 'VALID IF
for each bx in myarray
if myarray[y].bx = controlarray[i] 'COMPARE LOOP
while r < 5
?"MATCH. . . "
pinfo={
bx: myarray[r].bx
by: myarray[r].by
bt: myarray[r].bt
}
finalarray.Push(pinfo)
r=r+1
if r > 5
r=0
exit while
endif
end while 'R LOOP
else
y=y+1 'move to next myarray
end if 'COMPARE LOOP

end for
y=0

else 'INVALID

i=i+1

end if 'VALID IF


end for

Now, not completely sure what I am missing but as shown it kicks out after r reaches 5 as desired but it never rolls the controlarray or myarray to next number to continue. Not sure if it matters but not every myarray has 5 matching bx elements, the myarray is an array read from an array file structured like but with more elements than shown. The script checks to make sure the controlarray is valid, then compares the bx element to the controlarray. All works except once r reaches 5 it kicks out of all loops. If the myarray doesn't have 5 matches no problem but most will have more, I only want first 5 of each. Thanks


<?php
error_reporting(0);
$items=array (
0 =>
array (
'bx' => 'D100',
'by' => 'Dog',
'bt' => 'Brown',
),
1 =>
array (
'bx' => 'D100',
'by' => 'Dog',
'bt' => 'Black',
),
2 =>
array (
'bx' => 'D100',
'by' => 'Dog',
'bt' => 'white',
),
3 =>
array (
'bx' => 'C100',
'by' => 'Cat',
'bt' => 'white',
),
4 =>
array (
'bx' => 'C100',
'by' => 'Cat',
'bt' => 'Brown',
),
0 Kudos
3 REPLIES 3
RokuMarkn
Visitor

Re: Untangle Loops

I've only spent a few minutes looking at this but I can't say I'm completely understanding your logic. There are several things in the code that look strange to me.

1. You use both r and y before they have been initialized (maybe they're initialized in earlier code that you didn't quote?).
2. When controlarray = invalid you increment i, thus skipping over the entry that follows the invalid one, since i is already increment by the for loop.
3. You have a "for each bx..." statement but you never use the bx variable.

--Mark
0 Kudos
btpoole
Channel Surfer

Re: Untangle Loops

Thanks Mark, here is explanations for your questions.
If the controlarray is invalid the entire loop is skipped and the script increments the i to move to the next controlarray, there is no sense i entering the loops if controlarray is invalid, it will throw an error. The y and r were initialized earlier in code not posted. The bx is used directly after the For each in the If myarrary.bx statement.


Edit. Sorry Mark, I misunderstood your question about the controlarray. You are correct, I don't think I need to increment the i since it's already being done so.
0 Kudos
RokuMarkn
Visitor

Re: Untangle Loops

"btpoole" wrote:
The bx is used directly after the For each in the If myarrary.bx statement.


The expression myarray.bx does not use the bx variable. It uses a member of the myarray AA with the name "bx".

--Mark
0 Kudos