# using arrays and for loops

**URL:** <https://tech-community.robotics.abb.com/t/using-arrays-and-for-loops/11066>\
**Category:** RAPID Programming\
**Tags:** array, rapid-code\
**Created:** [March 15, 2023, 1:47pm UTC](https://tech-community.robotics.abb.com/t/using-arrays-and-for-loops/11066 "2023-03-15T13:47:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke1995](https://avatars.discourse-cdn.com/v4/letter/l/50afbb/32.png) [@Luke1995](https://tech-community.robotics.abb.com/u/Luke1995)\
**Post date:** [March 15, 2023, 1:47pm UTC](https://tech-community.robotics.abb.com/t/using-arrays-and-for-loops/11066/1 "2023-03-15T13:47:09Z")

</div>

Afternoon,

I am familiarising myself with Arrays for the first time and was wondering if anyone had a tidy way to solve the following issue;

I am populating an array with robtargets so that the robot can plot a path back to a home position. However depending on the position the robots has to navigate home from the array is not always entirely populated.

when I run my final bit of code:

FOR i FROM 1 TO DIM(aHomePath,1) DO  
MoveL aHomePath{i},v100,fine,tool0;  
WaitUntil\InPos, TRUE;  
ENDFOR

Obviously I get an error when the array is not entirely populated.

Is there a function to either ignore items in an array without any value? or failing this, rather than declaring my array as:

VAR Robtarget HomePath{10};

Could could i make the declaration of how many items are in the list variable so there is only ever the amount of items in the Array that the robot needs to navigate home?

Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![Tompanhuhu](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/tompanhuhu/32/3366_2.png) [@Tompanhuhu](https://tech-community.robotics.abb.com/u/Tompanhuhu)\
**Post date:** [March 15, 2023, 2:26pm UTC](https://tech-community.robotics.abb.com/t/using-arrays-and-for-loops/11066/2 "2023-03-15T14:26:39Z")

</div>

This could work:

```auto
    FOR i FROM 1 TO DIM(aHomePath,1) DO 
        IF aHomePath{i}.trans <> [0,0,0] THEN
            MoveL aHomePath{i},v100,fine,tool0;
            WaitUntil\InPos, TRUE;
        ENDIF
    ENDFOR

```

If the robtarget is empty, it will be ignored.  
Make the array the maximum size you will need, you can’t edit array sizes during runtime.

---

<div class="post-metadata">

**Author:** ![Luke1995](https://avatars.discourse-cdn.com/v4/letter/l/50afbb/32.png) [@Luke1995](https://tech-community.robotics.abb.com/u/Luke1995)\
**Post date:** [March 15, 2023, 3:28pm UTC](https://tech-community.robotics.abb.com/t/using-arrays-and-for-loops/11066/3 "2023-03-15T15:28:03Z")

</div>

Thanks for your time and appreciate the advice 🙂

I had thought of something similar, I was just interested to see if their was a more efficient way.

Not that it matters as this is a working solution, but if the array was declared as size {20} and only 2 positions were required to safely navigate home this causes a small delay whilst in the FOR loop.

---

<div class="post-metadata">

**Author:** ![Tompanhuhu](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/tompanhuhu/32/3366_2.png) [@Tompanhuhu](https://tech-community.robotics.abb.com/u/Tompanhuhu)\
**Post date:** [March 15, 2023, 7:14pm UTC](https://tech-community.robotics.abb.com/t/using-arrays-and-for-loops/11066/4 "2023-03-15T19:14:29Z")

</div>

You can put the forloop in a separate proc and return when you find the first empty robtarget.

---

<div class="post-metadata">

**Author:** ![Forge\_Engineering](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/forge_engineering/32/4455_2.png) [@Forge\_Engineering](https://tech-community.robotics.abb.com/u/Forge_Engineering)\
**Post date:** [March 16, 2023, 8:55pm UTC](https://tech-community.robotics.abb.com/t/using-arrays-and-for-loops/11066/5 "2023-03-16T20:55:05Z")

</div>

I’ve had the same thing before with storing information about unknown quantities, I usually store the index of the last item in the array so I know when I hit that index it will finish. I also find that useful because I can view that value in program data or remotely and see what the current index is and the final index as opposed to having to look at the array and figure out how many iterations are left. This is for dealing with large fillet welds up to ~90 passes, it might not be useful in your application to see that.

Good Luck.

---

<div class="post-metadata">

**Author:** ![mandolas](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/mandolas/32/2496_2.png) [@mandolas](https://tech-community.robotics.abb.com/u/mandolas)\
**Post date:** [February 16, 2024, 4:28pm UTC](https://tech-community.robotics.abb.com/t/using-arrays-and-for-loops/11066/6 "2024-02-16T16:28:18Z")

</div>

Hi…  
This is a very pertinent ‘dynamic array’ question.

I share a possible solution if there is still a need.

```auto
MODULE Testing
    ! Program 1
    PROC PRG1()
        VAR Robtarget HomePath{10};
        
        ! Move at HOME ...
        RobotHome HomePath;
    ENDPROC
    
    ! Program 2
    PROC PRG2()
        VAR Robtarget HomePath{2};
        
        ! Move at HOME ...
        RobotHome HomePath;
    ENDPROC
    
    ! Program 3
    PROC PRG3()
        VAR Robtarget HomePath1;
        VAR Robtarget HomePath2;
        VAR Robtarget HomePath3;
        
        ! Move at HOME ...
        RobotHome [HomePath1,HomePath2,HomePath3];
    ENDPROC
    
    ! Return at HOME
    PROC RobotHome(robtarget P_PathsP{*})
        FOR id FROM 1 TO Dim(P_PathsP,1) DO
            MoveJ P_PathsP{id},v100,z50,tool0;
        ENDFOR
    ERROR
        TPErase;

        TEST ERROR
        CASE ERR_OVERFLOW:
            ErrWrite "ERR_OVERFLOW",""
                \RL2:="Time memory overflow."
                \RL3:="Time measurements have been reset and the counter has been stopped."
                \RL4:="After restarting, the error counters will be restarted.";

            Stop;
            Stop;

            EXIT;
        DEFAULT:
            ErrWrite "NOT TREATED","Heating path - incorrect"
                \RL2:="Error not handled by the system. Please contact the automation team.";
        ENDTEST
    ENDPROC
ENDMODULE

```

Good works.
