1

I have a for loop in which I would like to create list and add values to there.

FOR ${i} IN RANGE 3

*Create list List_${i}

  • Add values to list

END

How could I do it? So that after exit the for loop, I would have list_1 & list_2 and list_3

Meaning I can create dynamic variable like this:

FOR  ${idx}  IN RANGE  3
    ${var_name} =      Catenate  SEPARATOR=_  var      ${idx}
    Set Suite Variable  ${${var_name}}  ${idx}
END

Maybe it is some easy way to do the same with @list ?

This does not work:

FOR    ${i}    IN RANGE     3
    Log    ${i}
    ${var_name}=  Catenate  SEPARATOR=_  TEST_NAME  ${i}
    Log    ${var_name}
    @{${${var_name}}}=    Create List    data
END
ZeroHero
  • 21
  • 5
  • does this answer your question? https://stackoverflow.com/questions/59420396/robotframework-how-to-define-a-dynamic-variable-name – Jiri Janous May 19 '22 at 06:51

2 Answers2

1

I think this is the answer:

FOR    ${i}    IN RANGE     3
    Log    ${i}
    Set Suite Variable    @{List${i}}    @{EMPTY}
    Append To List    ${List${i}}    ${i}
END
ZeroHero
  • 21
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 22 '22 at 00:57
0

Note sure if this is exactly what you are looking for; but, I'll try to help. First, lists cannot be empty; so you will need at least an initial value which you can then remove (or ignore) when you iterate thru the list. That said, I can suggest you create a For-Loop which returns is List-of-Lists since your "loop range may change".

Test-Dynamic-Lists-Creation

${listOfLists}    Create List    initial-value
FOR    ${index}    IN RANGE    3
    ${List_$index}    Create List    initial-value
    Append To List    ${listOfLists}    ${List_$index}
END
Comment    Removes the first value from the list.
Remove From List    ${listOfLists}    ${0}
Log List    ${listOfLists}
Return From Keyword    ${listOfLists}
  • I intended to add the above code as a *TEST* which you could just copy/paste/save/run; but Stackoverflow did not allow me to publish it as such. Still, you should be able to copy this code into a test and run it. – user2196798 May 24 '22 at 14:27