bildwelt
E2E FORUM
E2E Bridge E2E Commerce

array tips and tricks

E2E Forum Modeling & Development array tips and tricks

This topic contains 6 replies, has 6 voices, and was last updated by  Jens Ch. 2 months, 2 weeks ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2912

    Alfred
    Moderator

    Today we added two useful code snippets to learn from regarding array handling

    empty array
    set array = select each from array where false;

    appendcopy all content from array1 to array2
    set array2 = select each from array1 where true;

    What is your favorite, clever or ugly array handling code?

    • This topic was modified 2 months, 2 weeks ago by  Alfred.
    #2913

    Jens Ch.
    Moderator

    Thanks for the snippets Alfred!

    I set an empty array like this
    set array = NULL;

    And to just copy an array
    set array2 = array1;

    To really append array1 to array2 i used
    set array2 = array2.concatArrays(array1)

    #2914

    Kirstin
    Keymaster

    set array2 = select each from array1 where true;

    This copies array1 to array2 and replaces the content of array2 (instead of appending). To really append use concatArrays() as Jens suggested.

    #2916

    Dirk
    Moderator

    Hi Jens,

    create an empty array:
    set array = NULL;
    this will not result in an empty array, but in NULL. You won’t be able to append to array (in fact you will get a NULL pointer exception). But it should be possible to append to an empty array. I suggest Alfred’s solution here.

    copy an array:
    set array2 = array1;
    this will not create an array copy, but array2 will refer to the same array as array1 does. Again, use Alfred’s code snippet when you really want a copy of the array.

    Hope this helps,
    Dirk

    #2918

    Jens W.
    Participant

    I’m not too much into ActionScript arrays so far, but I think it would be great to make the condition optional, at least for the creation of an array copy:
    set array2 = select each from array1;

    #2919

    Jakub
    Moderator

    If you want to simply copy an array, use:
    set array2 = array1.copy();

    #2920

    Jens Ch.
    Moderator

    Hi Dirk,

    thanks for the hint with the reference, i wasn’t aware of that.
    However i can set an array to NULL and append the items of it without an error message.

    #2922

    Kirstin
    Keymaster

    A very interesting discussion, indeed. I made an example model and tried out the operations mentioned in this thread.
    Find below a recap of what exactly the operations do in array context:

    Empty Array

    set array = select each from array where false;

    A new array is created containing all references to elements of array that correspond to the condition (which are none). The reference of array is changed to point to the new array.

    What the modeler sees: array is empty now. You can append items using the append statement.

    set array = NULL;

    The reference of array is changed to point to nowhere.
    What the modeler sees: A non-existent array. You can append items using the append statement, the array will implicitly be created, then.

    Caution: Arrays within arrays
    Let’s assume you have an array of complex type ArrayElement and this complex type has a property subArray with multiplicity 0..*.

    • What you can do, if subArray is NULL:
      set array1[0].subArray = anotherArray;
      The reference subArray is changed to point to anotherArray.

    • What you can’t do, if subArray is NULL:
      append "something" to array1[0].subArray;
      In this case (get statement on the right side of a statement), the Runtime will throw a get error for array1[0].subArray.


    Copy Array Content

    set array2 = select each from array1 where true;

    A new array is created containing all references to elements of array1 that correspond to the condition (which are all). The reference array2 is changed to point to the new array.

    What the modeler sees: Two arrays array1 and array2 that contain the same element references. A change to array1 itself will not change array2 (e.g. appending elements), but a change to an element of array1 will change that very same element in array2.
    set array2 = array1.copy();

    A new array is created that contains a true deep copy of all elements of array1. The reference array2 is changed to point to the new array.
    What the modeler sees: Two arrays array1 and array2 that are completely independent. A change to array1 in any way will not change array2.


    Appending
    Arrays

    set array2 = array2.concatArrays(array1)

    A new array is created containing all references to elements of array1 and array2. The reference array2 is changed to point to the new array.

    What the modeler sees: An array array2 containing all element references of array1 and array2. A change to array1 itself will not change array2 (e.g. appending elements), but a change to an element of array1 will change that very same element in array2.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.