RobotStudio event

Using array index to make assignments based on index position.

I am trying to use the index position of an array of strings to assign Position Values based on the index position ({1}  = "A" when "A" assign A_values(x,y,z)), but I can not figure out how to iterate through a list either with a FOR loop, or a TEST loop because it won't allow me to call 'string_var{i}' to utilize the complete index. 

Ex. 

Pin{3} := ["A","B","C"];

Pin_Num := 0;

FOR i FROM 1 TO 3 DO
   Pin_Num := Pin{i};
ENDFOR


The Pin gets chosen on the pendant so the array on Pin is linked to a screen but once its is picked I can't use it to do anything because whenever I try to assign it just says it was expecting another string. I am sure the "StrToVal()" function may also be useful but after trying a few different attempts, I am once again running into not being able to iterate through the array of Pin{}.

Ex.

Pin{3} := ["A","B","C"];

Pin_Num := 0;

FOR i FROM 1 TO 3 DO
   StrToVal(Pin{i},Pin_Num);
ENDFOR 

Obviously, I am missing something major here but I have to believe this is possible, I just am not having any luck with the documentation having any examples of something like this. 

Any help is greatly appreciated or even just a point in the right direction!
Thank you,

Answers

  • I'm not completely following what you're trying to do. However, I can explain you why this is not working.

    The array `Pin{}` that you created contains three strings: "A", "B" and "C". In the FOR loop you're essentially doing this:
    Pin_Num := "A";
    Pin_Num := "B";
    Pin_Num := "C";

    Your `Pin_Num` variable has to be a number, not a string. Also, the `Pin_Num` variable is overwritten at every iteration.
  • SomeTekk
    SomeTekk
    edited February 2023
    As nicolahinssen pointed out the data types are mismatched. 

    Also, from the technical manual for RAPID:

    Syntax
    StrToVal’(’
    [ Str ’:=’ ] <expression (IN) of string> ´,´
    [ Val ’:=’ ] <var or pers (INOUT) of ANYTYPE>
    ’)’
    A function with a return value of the data type bool.

    After addressing the data type issue and with the bolded text above in mind perhaps reworking your code as below may help move your project forward.

    ok:=StrToVal(Pin{i},Pin_Num);

    A TPReadFK, or UI ... (instruction or function) could be used to gather the necessary input.

    Another method to consider is late binding. Check out the RAPID Overview manual and search for late binding. 

    Sample:

        CONST string strNames{3}:=["rA","rB","rC"];

        PROC rYourProc()
           ! Determine your value for the array
                %strNames{index #}%;
       ENDPROC

        PROC rA()
            ...
        ENDPROC

        PROC rB()
            ...
        ENDPROC

        PROC rC()
           ...
        ENDPROC
    Post edited by SomeTekk on