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.
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.
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.