Table Of Contents

Repeating Operations Once for Every Element in an Array

Version:
    Last Modified: March 8, 2017

    While working with an array of data, you might want to access individual elements within the array. Although you can use a combination of Array nodes to accomplish this task, the For Loop includes an auto-indexing tunnel that you can use to access the individual elements with minimal additional code.

    Use a For Loop with an auto-indexing input tunnel to process one element of an array during each iteration of the loop. A For Loop with an auto-indexing input tunnel behaves similarly to a for each loop in other programming languages.

    What to Use

    For Loop with an auto-indexing input tunnel

    What to Do

    Create the following diagram to repeat an operation once for every element in an array.

    Customize the gray sections for your unique programming goals.

    Place the code you want to repeat on the subdiagram of a For Loop.
    When you wire an array to a For Loop, the For Loop processes one element of the array at a time. This configuration, known as auto-indexing, occurs by default. The input tunnel appears as a white box with brackets to indicate that it is an auto-indexing tunnel.
    spd-note-note
    Note  

    A For Loop can process multiple arrays one element at a time using multiple auto-indexing input tunnels. In this situation, the loop uses the smallest array size to determine the number of loop iterations. For example, if two auto-indexed arrays enter the loop with 10 and 20 elements respectively, the loop executes 10 times, processing all elements of the first array but only the first 10 elements of the second array.

    You can access each individual element of the input array by wiring the auto-indexing tunnel to the code on the subdiagram of the loop. The wire entering the auto-indexing tunnel carries 1D array data, whereas the wire leaving the auto-indexing tunnel carries scalar data.
    spd-note-note
    Note  

    For multidimensional input arrays, the wire leaving the auto-indexing tunnel carries array data that is one dimension smaller than the input array. For example, if the input array terminal passes a 2D array to the auto-indexing tunnel, the wire leaving the auto-indexing tunnel carries 1D array data.

    By not wiring a value to the count terminal when auto-indexing is enabled, the loop automatically iterates once for each element in the array.

    spd-note-note
    Note  

    Wiring a value to the count terminal while auto-indexing is enabled causes the For Loop to use the smallest of the choices between the count terminal and the input array size to determine the number of loop iterations. For example, if an auto-indexed array enters the loop with 10 elements and you wire a value of 15 to the count terminal, the loop executes 10 times.

    If you want to collect the result of each loop iteration in an array, use an auto-indexing output tunnel to pass values out of the loop. To enable auto-indexing for an output tunnel, right-click the tunnel and select Append Mode»Auto Index Values. If you do not enable auto-indexing, the loop returns only the value from the last loop iteration.

    Troubleshooting

    • If the For Loop iterates an unexpected number of times, note that if you enable auto-indexing for more than one input tunnel or if you wire a value to the count terminal, the actual number of loop iterations becomes the smallest of the choices.
    • If the For Loop processes the entire input array in a single loop iteration instead of one element per iteration, verify that the input tunnel is auto-indexing the array. An auto-indexing tunnel appears as a white box with brackets, whereas a non-indexing tunnel appears as a solid box.
    • If the execution speed of the loop is too fast, place a Wait node on the subdiagram of the loop to specify a wait period.

    Examples

    Use Search to find and open the following installed lesson: For Loops


    Recently Viewed Topics