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
Tags (1)
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
Tags (1)
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.
Tags (1)
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.
Tags (1)
0 Kudos
DLC
Visitor

Re: Know if an element exists

Only one word : perfect 😄
That is working perfectly fine now, many thanks to you.
Tags (1)
0 Kudos
Community is Temporarily in Read-Only Mode!

We’re upgrading Roku Community to bring you a faster, more mobile-friendly experience. You may notice limited functionality or read-only access during this time. You will not be able to log in or post new comments or kudos during this time. Read more here.

Planned Downtime:
Community will be unavailable for up to 24–48 hours during the upgrade window during the week of May 12 and you may notice reduced functionality. In the meantime, for additional assistance, visit our Support Site.

Thanks for your patience — we’re excited to share what’s next!