ZeroHero
ZeroHero

Reputation: 21

Robot Framework dynamic list variable

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}

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

Upvotes: 1

Views: 1127

Answers (2)

user2196798
user2196798

Reputation: 39

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}

Upvotes: 0

ZeroHero
ZeroHero

Reputation: 21

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

Upvotes: 1

Related Questions