[Closed] update-ble global array
Please help
I will try to explain, but I’m afraid not to confuse you:
I have some opperations that takes time on a global array that may be updated from other scripts
global myarray =
#(
#(true, data1, data2,...)
#(true, data1, data2,...)
#(false, data1, data2,...)
#(true, data1, data2,...)
....
)
fn operationitself_fn =
(
-- during executing this function, myarray may be updated
...
)
for i = 1 to myarray.count do
(
-- sort myarray - I need to apply operationitself_fn() to the items with true at the beginning first and just after this to all other items
operationitself_fn()
deleteItem myarray i
)
problems:
#if I use [myarray.count] is not correct because this count may be changed while operationitself_fn()
#if I use [for s in myarray do] then how to deleteitem?
#how to apply operationitself_fn() to the items with "true" at the beginning - somehow to verify at every iteration - are there such elements or not, and just after this to other elements
I think something like
while myarray.count> 0 do
(
for i=1 to myarray.count do
if myarray[i][1] == true
then
(
print i
operationitself_fn myarray[i]
deleteItem myarray i
)
else
(
operationitself_fn myarray[i]
deleteItem myarray i
)
)
but I’m doing smth wrong
how do you think, is this right?
while myarray.count> 0 do
(
for i=1 to myarray.count do
(
if myarray[i][1] == true
then
(
print i
operationitself_fn myarray[i]
deleteItem myarray i
)
)
for i=1 to myarray.count do
(
operationitself_fn myarray[i]
deleteItem myarray i
)
)
I am not sure how your function works, but whenever you need to delete an item from an array while at the same time you are iterating over that array, you need to iterate in reverse order so you dont run out of bounds.
For example this will produce an out of bounds error:
(
arr = for j = 1 to 20 collect j
for j = 1 to arr.count where mod j 2 == 0 do deleteitem arr j
arr
)
Doing the same in reverse order will work:
(
arr = for j = 1 to 20 collect j
for j = arr.count to 1 by -1 where mod j 2 == 0 do deleteitem arr j
arr
)
Does something like this work for you?
(
arr = for j = 1 to 20 collect if (random 1 2) == 1 then true else (random black white)
fn operationitself_fn arg1 arg2 = format "% %
" arg1 arg2
done = #{1..arr.count}
for j in done where arr[j] == true do
(
operationitself_fn j true
done[j] = false
)
for j in done do operationitself_fn j arr[j]
)
Edited: Reading your description again I realized this won’t work as you said that operationitself_fn() can modify the items in the array
It is unclear what the operationitself_fn() function does with the array. You mention it may update the array, but not what changes does it do to the array.
I assumed two things, it does not remove elements and it can add true and false elements.
Perhaps I overcomplicated the problem, so it would be useful if you could explain a little more about what the operationitself_fn() function does.
Considering this, and if you always need to process the “true” elements first, the following code could work.
The code will process all items with a “true” value first and then the remaining items. As soon as a new item with “true” is added it will give priority to that item over the others, or something like that.
Perhaps the sorting inside the loop could be removed if not needed.
If the concept works as you need, it can be rewritten in a clearer way.
(
local arr = for j = 1 to 20 collect if (random 1 2) == 1 then #(true,1,1,1) else #(false,0,0,0)
fn SortByBoolean i1 i2 = if i1[1] == true then -1 else 1
fn operationitself_fn =
(
case (random 1 3) of
(
1: append arr #(true,1,1,1)
2: append arr #(false,0,0,0)
)
)
qsort arr SortByBoolean
while arr.count > 0 do
(
while arr.count > 0 and arr[1][1] == true do
(
operationitself_fn()
deleteitem arr 1
qsort arr SortByBoolean
)
while arr.count > 0 and arr[1][1] == false do
(
operationitself_fn()
deleteitem arr 1
qsort arr SortByBoolean
)
)
)
Oh, Jorge, Thank youuuuu! I think your last post is the right solution. I will try it soon. I’m so happy now! Thank you very much!