How to save variables from a loop

Hello everyone,

How do I save the “scheduled on” dates to different variables like date1, date2, and so on using a loop(not started from the loop button)?

It can be done manually but if the number of rows go higher, it will be tiresome.

schedule

{ "Command": "storeText", "Target": "xpath=//*[@id=\"0\"]/div/table/tbody/tr[${!LOOP}]/td[4]", "Value": "class" },

The text is located in the 2nd row and goes all the way to row 13.

What is the best way to get this done?

Thank you :slight_smile:

Use the TIMES…END loop and then use !TIMES instead of !LOOP

And instead of using many different variables, use an array. Arrays are a bit tricky to work with, but once you have the right code in place it is actually quite easy and saves many lines of code:

Test macro:

{
  "Name": "times and array",
  "CreationDate": "2020-11-13",
  "Commands": [
    {
      "Command": "comment",
      "Target": "Define array",
      "Value": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var arr = []; return arr",
      "Value": "array1"
    },
    {
      "Command": "times",
      "Target": "3",
      "Value": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var newArr = ${array1}; newArr[(${!times}-1)]  = ${!times}*2; return newArr",
      "Value": "array1"
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    }
  ]
}

Thank you @ulrich. Arrays sound like the best way in this case. I am not very good at programming. How do I stores the dates in an array using a loop?

Thank you again for your help.

First store the text in a normal variable (just as you did already), and then assign this variable to the array inside executeScript_Sandbox:

  • storeText | xpath=your xpath… | class

  • executeScript_Sandbox | var newArr = ${array1}; newArr[(${!times}-1)] = ${class}; return newArr; | array1

1 Like