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: 
DLC
Visitor

Know if an element exists

Hi everybody,

I have a double loop on an array in which each element is an array, like this:

for i=0 to 10
for j=0 to 5
print my_array[i][j]
end for
end for


The thing is that the size of the elements in the big array is changing. For example, my_array[0].Count() = 6 and my_array[1].Count() = 5.
So when i do my loop i have an error when i=1 and j=5 as my_array[1][5] doesn't exist. The problem is that if i stop my second loop at j=4 i will lose the last element of my_array[0], and i don't want to.

So is there a way to check if the element exists?
I tried :

for i=0 to 10
for j=0 to 5
if Eval(my_array[i][j]) = 252
print my_array[i][j]
end for
end for

and

for i=0 to 10
for j=0 to 5
if Eval(my_array[i][j]<>invalid) = 252
print my_array[i][j]
end for
end for

and

for i=0 to 10
for j=0 to 5
if my_array[i][j]<>invalid
print my_array[i][j]
end for
end for

but it doesn't work.
How can i do please?
Thank you.

Denis

PS: i use 252 with Eval() because in the codes in this post, it is said that it is the success return value.
viewtopic.php?f=34&t=47110&p=320415&hilit=eval#p320415
0 Kudos
4 REPLIES 4
MSGreg
Visitor

Re: Know if an element exists

Try:
for i=0 to 10
for j=0 to my_array[i].Count()
print my_array[i][j]
end for
end for
0 Kudos
DLC
Visitor

Re: Know if an element exists

Sorry i forgot one detail, i am displaying the texts in a grid and i need to fill in the grid completely, with the text in my array if it exists, with "unavailable" if not.
If i use Count(), i will have empty spaces in the grid, right?
But thank you for answering.
0 Kudos
MSGreg
Visitor

Re: Know if an element exists

No problem. Try this:
for i=0 to 10
for j=0 to 5
if my_array[i].Count() > j then
print my_array[i][j]
else
' empty cell
end if
end for
end for


"invalid" in BrightScript is kind of a tricky concept. It's sort of equivalent to "null" or "nil" in other languages, but is not the same as "uninitialized". Plus in the case of arrays, I don't think you can even access beyond the end of the array to be able to do the comparison to "invalid".

It all depends on how you've "Dim'd"/declared them. If you, for example, have declared every array the same size, then you should be able to access the values at each location in the array representing the grid. I'm not sure what the value in the array that is declared but not assigned, though I probably would not rely on that "initial value", unless documented.

Good Luck! Hope this helps.
0 Kudos
DLC
Visitor

Re: Know if an element exists

Only one word : perfect 😄
That is working perfectly fine now, many thanks to you.
0 Kudos