Declare dynamic array or change array size
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
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
0
Comments
-
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 apaCONST 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;ENDPROCPROC initarray()currentarraysize:=0;ENDPROCPROC insertelement(num element)currentarraysize:=currentarraysize+1;myarray{currentarraysize}:=element;ENDPROCPROC printarray()FOR i FROM 1 TO currentarraysize DOTPWrite "Element myarray{" + NumToStr(i,0) + "} = " + numtostr(myarray{i},0);ENDFORENDPROCENDMODULE[/CODE]Henrik Berlin
ABB0 -
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 ?
0 -
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.0
Categories
- All Categories
- 5.5K RobotStudio
- 396 UpFeed
- 18 Tutorials
- 13 RobotApps
- 297 PowerPacs
- 405 RobotStudio S4
- 1.8K Developer Tools
- 250 ScreenMaker
- 2.8K Robot Controller
- 316 IRC5
- 61 OmniCore
- 7 RCS (Realistic Controller Simulation)
- 800 RAPID Programming
- AppStudio
- 3 RobotStudio AR Viewer
- 18 Wizard Easy Programming
- 105 Collaborative Robots
- 5 Job listings