RobotStudio event

Declare dynamic array or change array size

Options
I need to define an array with a dimension that is previously calculated:

PROC myprogram(num arraysize)
       VAR num myarray{arraysize};
       ...
ENDPROC

RAPID returns an error saying arraysize isn't a constant. So is there any possibility or workaround to achieve that? Or is there even an instruction to append dimensions to an array?

Regards,
Michael

Comments

  • Henrik Berlin
    Options

    Varibles cannot be dynamically allocated during run-time in RAPID. What you can do is to declare the array to be large, 128, say. And then use a num variable to point out the last valid element. Something like this perhaps

    [CODE]
    MODULE apa

    CONST num maxarraysize := 128;
    VAR num  myarray{128};
    VAR num arraysize:=0;
    VAR num currentarraysize:=0;


    PROC main()
    initarray;
    insertelement(2);
    insertelement(45);
    insertelement(673);
    printarray;
    ENDPROC

    PROC initarray()
    currentarraysize:=0;
    ENDPROC

    PROC insertelement(num element)
    currentarraysize:=currentarraysize+1;
    myarray{currentarraysize}:=element;
    ENDPROC

    PROC printarray()
    FOR i FROM 1 TO currentarraysize DO
    TPWrite "Element myarray{" + NumToStr(i,0) + "} = " + numtostr(myarray{i},0);
    ENDFOR
    ENDPROC
    ENDMODULE
    [/CODE]
    Henrik Berlin
    ABB
  • Daper
    Options
    Hello @Henrik Berlin or anyone else,

    I am facing exactly the same problem.
    It seems like it is still the same 15 years later, but has a new workaround been developped since then ?
    Is there any alternative anyone konws about, except from oversizing the array at the beginning ?

  • mandolas
    mandolas
    edited February 5
    Options
    Hi...

    When the encounter is a problem I use Array as a parameter...

    See the example to read the file...
    	PROC FileContentRead(INOUT string contentP{*},string pathFileP)
             ! ## ATTRIBUTES ###########################################################################
             VAR iodev fileIodevReadL;
             VAR num indexL:=1;
             ! ## ATTRIBUTES ###########################################################################
    
    
             ! Open the file for Writing...
             Open pathFileP,fileIodevReadL\Read;
    
    		 ! Iterates through the array for reading...
             WHILE indexL<=Dim(contentP,1) DO
                 contentP{indexL}:=ReadStr(fileIodevReadL\RemoveCR\DiscardHeaders);
    
                 Incr indexL;
             ENDWHILE
    
             Close fileIodevReadL;
    
             RETURN ;
         ERROR
             ! ################################################ #########################################
             ! HANDLING OF POSSIBLE ERRORS
             ! ################################################ #########################################
    
             ! If an error is found, it displays a message and exits the program...
             IF ERRNO=ERR_FILEACC THEN
                 ! Clean the screen...
                 TPErase;
    
                 ErrWrite "ERR_FILEACC","Error reading file"
                     \RL2:="An error occurred while reading the parameter file."
                     \RL3:="Action:"
                     \RL4:="Check the file syntax.";
    
                 ! Close the file.
                 Close fileIodevReadL;
    
                 Stop;
                 Stop;
    
                 EXIT;
             ENDIF
         ENDPROC
    Good Works.