cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

Does anyone have examples of using arrays in SmartAssembly. I am struggling with the admin guide examples.

rschoenhals
2-Explorer

Does anyone have examples of using arrays in SmartAssembly. I am struggling with the admin guide examples.

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


This thread is inactive and closed by the PTC Community Management Team. If you would like to provide a reply and re-open this thread, please notify the moderator and reference the thread. You may also use "Start a topic" button to ask a new question. Please be sure to include what version of the PTC product you are using so another community member knowledgeable about your version may be able to assist.
4 REPLIES 4
TomU
23-Emerald IV
(To:rschoenhals)

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.

TomU
23-Emerald IV
(To:TomU)

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_FOR

FOR 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

TomU
23-Emerald IV
(To:TomU)

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

rschoenhals
2-Explorer
(To:TomU)

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


Announcements
NEW Creo+ Topics: Real-time Collaboration


Top Tags