Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X
I do not understand why the DELETE_ARRAY_ELEM won't work....
SEARCH_DRW_VIEWS CurrentDrawing "*" "*" ViewArray
DECLARE_VARIABLE INTEGER DeleteIndex 0
FOR EachView REF ARRAY ViewArray
GET_DRW_VIEW_ORIENTATION EachView ViewOrientation
PRINT "before_EachView = % ViewOrientation = %" EachView ViewOrientation
END_FOR
!@@@ error checking for background and overlay views....
FOR EachView REF ARRAY ViewArray
GET_DRW_VIEW_ORIENTATION EachView ViewOrientation
IF ViewOrientation == "UNKNOWN"
DELETE_ARRAY_ELEM ViewArray DeleteIndex
END_IF
DeleteIndex++
END_FOR
FOR EachView REF ARRAY ViewArray
GET_DRW_VIEW_ORIENTATION EachView ViewOrientation
PRINT "after_EachView = % ViewOrientation = %" EachView ViewOrientation
END_FOR
I think as soon as you execute DELETE_ARRAY_ELEM, the number of elements will shift and your DeleteIndex will no longer agree with the current array element.
Actually, I don't think that's it. It looks like you cannot use DELETE_ARRAY_ELEM when inside a FOR loop that is processing that same array. Here is the sample code I used to test this:
DECLARE_ARRAY TEST_ARRAY
ADD_ARRAY_ELEM TEST_ARRAY 0
ADD_ARRAY_ELEM TEST_ARRAY 1
ADD_ARRAY_ELEM TEST_ARRAY 2
ADD_ARRAY_ELEM TEST_ARRAY 3
ADD_ARRAY_ELEM TEST_ARRAY 4
DECLARE_VARIABLE INTEGER DeleteIndex 0
FOR EACH_ELEM REF ARRAY TEST_ARRAY
PRINT "Loop 1 Value: %, Index Value: %" EACH_ELEM DeleteIndex
IF EACH_ELEM == "1" OR EACH_ELEM == "3"
DELETE_ARRAY_ELEM TEST_ARRAY DeleteIndex
END_IF
DeleteIndex ++
END_FORFOR EACH_ELEM REF ARRAY TEST_ARRAY
PRINT "Loop 2 Value: %" EACH_ELEM
END_FOR
DELETE_ARRAY_ELEM TEST_ARRAY 3
FOR EACH_ELEM REF ARRAY TEST_ARRAY
PRINT "Loop 3 Value: %" EACH_ELEM
END_FOR
Correction. It does work fine. I was checking for a string instead of an integer. (Thanks to Hatim from Sigmaxim.) The compare line should say:
IF EACH_ELEM == 1 OR EACH_ELEM == 3
Also, removing a array element instantly changes the array size, so you need to decrement the DeleteIndex variable accordingly:
IF EACH_ELEM == 1 OR EACH_ELEM == 3
DELETE_ARRAY_ELEM TEST_ARRAY DeleteIndex
DeleteIndex = DeleteIndex - 1
END_IF
Final code:
BEGIN_ASM_DESCR
BEGIN_CATCH_ERROR
DECLARE_ARRAY TEST_ARRAY
ADD_ARRAY_ELEM TEST_ARRAY 0
ADD_ARRAY_ELEM TEST_ARRAY 1
ADD_ARRAY_ELEM TEST_ARRAY 2
ADD_ARRAY_ELEM TEST_ARRAY 3
ADD_ARRAY_ELEM TEST_ARRAY 4
DECLARE_VARIABLE INTEGER DeleteIndex 0
FOR EACH_ELEM REF ARRAY TEST_ARRAY
PRINT "Loop 1 Value: %, Index Value: %" EACH_ELEM DeleteIndex
IF EACH_ELEM == 1 OR EACH_ELEM == 3
DELETE_ARRAY_ELEM TEST_ARRAY DeleteIndex
DeleteIndex = DeleteIndex - 1
END_IF
DeleteIndex ++
END_FOR
FOR EACH_ELEM REF ARRAY TEST_ARRAY
PRINT "Loop 2 Value: %" EACH_ELEM
END_FOR
DELETE_ARRAY_ELEM TEST_ARRAY 1
FOR EACH_ELEM REF ARRAY TEST_ARRAY
PRINT "Loop 3 Value: %" EACH_ELEM
END_FOR
END_CATCH_ERROR
END_ASM_DESCR
I believe the (DeleteIndex – 1) approach would not work on the first one you delete and if you skipped two or more without deleting. Too be safe I took the following approach and made a temporary array of “keepers” then I clear out the original array and put the “keepers” in it.
It seems strange there isn’t a more robust way to remove elements of an array though.
Thanks for the discussion, your approach did help me get to what I needed to do. Take care.
BEGIN_ASM_DESCR
DECLARE_ARRAY arrayOriginal
DECLARE_ARRAY arrayKeepers
ADD_ARRAY_ELEM arrayOriginal "dog"
ADD_ARRAY_ELEM arrayOriginal "cat"
ADD_ARRAY_ELEM arrayOriginal "mouse"
ADD_ARRAY_ELEM arrayOriginal "fish"
ADD_ARRAY_ELEM arrayOriginal "frog"
FOR eachOriginal REF ARRAY arrayOriginal
PRINT "before eachOriginal = %" eachOriginal
END_FOR
FOR eachOriginal REF ARRAY arrayOriginal
PRINT "eachOriginal = %," eachOriginal
IF eachOriginal == "fish" or eachOriginal == "mouse"
ELSE
ADD_ARRAY_ELEM arrayKeepers eachOriginal
END_IF
END_FOR
CLEAR_ARRAY arrayOriginal
FOR eachKeeper REF ARRAY arrayKeepers
PRINT "after eachKeeper = %" eachKeeper
ADD_ARRAY_ELEM arrayOriginal eachKeeper
END_FOR
FOR eachOriginal REF ARRAY arrayOriginal
PRINT "after eachOriginal = %" eachOriginal
END_FOR
END_ASM_DESCR