2

I need to define a 2 dimensional array variable.
I have been able to use a list variable but now the project I'm working on requires an array.

How is this done?
How can I "loop" through the 2 dimensional array?

1
  • 2
    When you say "2 dimensional array", how exactly do you plan on using it? What are the dimensions? Are they integers? A list of lists is a two dimensional array, will that suffice? Does it have to be a literal 2 dimension array, or can you use a dictionary where the keys look two-dimensional? Can you give us an example of the type of data you need to manage? Commented Jul 30, 2014 at 16:18

4 Answers 4

4

Try using a list of lists. You can use the extended variable syntax to access an item in the inner list.

*** Settings ***
Library           Collections

*** Variables ***
@{colors}         red    green    blue
@{animals}        cow    pig    dog
@{things}         ${animals}    ${colors}

*** Test Cases ***
2-Dimensional List
    Log List    ${things}
    Log    The ${things[0][1]} is ${things[1][1]}

List length is 2 and it contains following items: 0: [u'cow', u'pig', u'dog'] 1: [u'red', u'green', u'blue']

The pig is green

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the great answer! One more thing, can I loop through the the dimensions: cow, red: cow, green.... pig, red.....?
I've been using this approach because it seems the only one that creates something close to an actual table. HOWEVER, it becomes a big problem if you have, for example, 256 colors. Either you have a huge line or, if you brake it, the table visualization is gone. IMHO, not having decent data tables is kind of a shame for the Robot Framework team.
2

Using lists

Robot's term for an array variable is "list". You can use @{...} to designate a variable as a list. Here's an example that shows how to create a list in a variable table, and how to do it within a test using the Create List keyword:

*** Variables ***
| # create a list in a variable table
| @{Colors} | red | orange | green | blue

*** Test Cases ***
| Example of using lists

| | # create an array inside a test
| | @{Names}= | Create list | homer | marge | bart

| | # Verify that the elements are arrays
| | Length should be | ${Colors} | 4
| | Length should be | ${Names} | 3

To create a two dimensional list, you can create a list of lists:

| | ${array}= | Create list | ${Names} | ${Colors}

The extended variable syntax lets you access individual elements:

| | log | element 1,1: ${array[1][1]}

For more information, see the section titled List variables in the Robot Framework User Guide

Using dictionaries

You can use a dictionary to simulate a multi-dimensional array. For example:

*** Settings ***
| Library | Collections

*** Test Cases ***
| Example of using a dictionary to simulate a two dimensional array
| | ${array}= | Create dictionary 
| | ... | 0,0 | zero, zero
| | ... | 0,1 | zero, one
| | ... | 1,0 | one, zero
| | ... | 1,1 | one, one
| | Should be equal | ${array["1,0"]} | one, zero

1 Comment

Thanks but I'm actually looking for a 2 dimensional array. Sorry I wasn't clearer about it in the post.
1

I found a way to loop through a list of lists:

*** Settings ***
Library           Collections

*** Variables ***
@{colors}         red    green    blue
@{animals}        cow    pig    dog
@{things}         ${animals}    ${colors}

*** Test Cases ***
Nested for loop example
    : FOR    ${x}    IN    @{animals}
    \    Keyword with for loop    ${x}

*** Keywords ***
Keyword with for loop
    [Arguments]    ${x}
    :FOR    ${y}    IN    @{colors}
    \    Log  The ${x} is ${y}

The credit goes to ombre42. Thanks!

Comments

0

Possible way to simulate a two dimensional array is to make a list of strings and split the string as you use it. The advantage over a list of lists is that each row is defined as a single line so you won't make mistakes when adding or deleting rows.

This array uses a space as the delimiter. Depending on your data you might have to use another delimiter.

*** Settings ***
Library                 String

*** Variables ***
# 3 columns per row
@{DATA}    1.0 2.2 30.0
...        2.0 5.5 1.0
...       32.0 6.1 1.4

*** Keywords ***
Using Line
    [Arguments]     ${line}
    @{data}=   Split String   ${line}
    Log   @{data}[0]   console=yes
    
*** Test cases ***
Using All Lines
    :FOR   ${line}   IN   @{DATA}
    \    Using Line   ${line}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.