multi dimensional arrays?

1,452 views
Skip to first unread message

David Smith

unread,
Jan 23, 2014, 1:13:57 PM1/23/14
to robotframe...@googlegroups.com
Are multidimensional arrays supported at all in robotframework?

Seems like the best example I can come up with so far;

| @{array_define} | array_name_1 | array_name_2 |
| @{array_name_1} | val1 | val2 | val3
| @{array_name_2} | val1 | val2 | val3

i can then iterate through the two arrays separately, also I can use a for loop and a substitute variable to call the two different arrays
${array_name_[${counter}]}

Is there a better way to handle this?

I can't find anything to handle arrays such as:
| @{array_name_[1]} | val1 | val2 | val3
| @{array_name_[2]} | val1 | val2 | val3

and referencing would be @{array_name_[1]}[1] or ${array_name_[1][1]}??


Kevin O.

unread,
Jan 24, 2014, 10:37:28 AM1/24/14
to robotframe...@googlegroups.com
I never use the @{var}[0] syntax because it is limited.
I always reference the list variable as a scalar and combine that with the extended variable syntax because it is more flexible.

Below is an example of defining and using nested lists.

*** Variables ***
@{inner list 1}    1    2    3
@{inner list 2}    6    7    8
@{outer list}     ${inner list 1}    ${inner list 2}    # surprisingly this can also be defined before the inner lists are

In some test case or keyword:
    Log    ${outer list[1][2]}

David Smith

unread,
Jan 24, 2014, 12:04:05 PM1/24/14
to robotframe...@googlegroups.com
Thanks for the example Kevin, it's very helpful.

Because of the nature of having to define the inner lists and outer lists, for the particular case I was hoping to use it, I don't think this simplifies things.
If you can define the outer list first, I assume this means that the inner lists are always reference by name, so if you were to go back and alter the values by inner list you'd automatically be updating the referenced values in the outer list, and vice versa?

David Smith

unread,
Jan 24, 2014, 12:52:45 PM1/24/14
to robotframe...@googlegroups.com
Another thought, is there a way to do this so that you call the inner lists by name instead of index #?

For example

Log ${outer list[1][someproperty]}
where the resulting output of the log is a list.



On Friday, January 24, 2014 7:37:28 AM UTC-8, Kevin O. wrote:

Kevin O.

unread,
Jan 24, 2014, 4:26:19 PM1/24/14
to robotframe...@googlegroups.com
As you may have noticed, nested objects may not always be the best way to represent data even if that is their logical relationship. As Pekka reminded me lately, the fifth tenet of the Zen of Python is: Flat is better than nested.

If you want to use names to refer to items of an object, list is not the correct type. You can use a dictionary for that. It will make your test data much more readable.
Dictionaries are very useful, but they cannot be created in the variables table. That capability is scheduled for 2.9.
For now you have to create the dictionary using keywords or in a variable file. Collections has a Create Dictionary keyword as well as other keywords for working with lists and dictionaries.

For your example you could:
${object}=    Create Dictionary    someproperty    42
${outer list}=    Create List    blah    ${object}
Log    ${outer list[1]['someproperty']}  # note that the key has to be in quotes
this logs 42

IMHO, this is much easier to express in a variable file:
OUTER_LIST = [
    u'blah',
    {'someproperty': '42'}
]

Another option for more complex testing scenarios is to use your own Python classes that use all or none of the below:
attributes e.g. ${person.phone_number}
indices e.g. ${listing[4]}
keys e.g. ${person['phone_number']}

Regarding your earlier post: if you update the inner list, the change would be reflected anywhere you have a reference to the inner list.
There are two keywords in Collections for copying: Copy List and Copy Dictionary. Note that they only make shallow copies. Thus, this test fails:

    ${list}=    Create List    1    2
    ${dict 1}=    Create Dictionary    list    ${list}
    ${dict 2}=    Copy Dictionary    ${dict 1}
    Insert Into List    ${dict 1['list']}    0    woah
    List Should Not Contain Value    ${dict 2['list']}    woah
Reply all
Reply to author
Forward
0 new messages