[Solved] [Issue #408] How do I change a value in an array?


#1

lets say I have this:
storeEval | new Array (“one”,“two”,“three”) | array

How do I change the value on the second item?

storeEval | storedVars[‘array’][2]=“five” |

doesn’t work


#2

Anyone can give some input, please?


#4

LoL.Have you tried it before posting? :grin:


#5

Here is a code snippet that first fills an array, and then changes a value with
storeEval | storedVars['names'][${num}] = "Hello World!"

{
  "CreationDate": "2018-10-30",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed"
    },
    {
      "Command": "open",
      "Target": "https://a9t9.com/kantu/demo/storeeval",
      "Value": ""
    },
    {
      "Command": "storeEval",
      "Target": "new Array ('cat','dog','fish','dog','??','frog','?','dog','??','horse','??elephant')",
      "Value": "names"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'].length",
      "Value": "length"
    },
    {
      "Command": "store",
      "Target": "2",
      "Value": "num"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'][${num}]",
      "Value": "myrandomname"
    },
    {
      "Command": "echo",
      "Target": "in array is ${myrandomname}",
      "Value": "blue"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'][${num}] = \"Hello World!\"",
      "Value": "myrandomname"
    },
    {
      "Command": "echo",
      "Target": "in array is now ${myrandomname}",
      "Value": "green"
    }
  ]
}

#6

This doesn’t change anything. This is the problem. You just assign $myrandomname a value FROM the array and call it for echo. JavaScript code doesn’t actually work in StoreEval just gets the void.
Check for yourself. Just call it once again. It is fish and always was. Sic!
{
“CreationDate”: “2018-10-31”,
“Commands”: [
{
“Command”: “store”,
“Target”: “fast”,
“Value”: “!replayspeed”
},
{
“Command”: “open”,
“Target”: “https://a9t9.com/kantu/demo/storeeval”,
“Value”: “”
},
{
“Command”: “storeEval”,
“Target”: “new Array (‘cat’,‘dog’,‘fish’,‘dog’,’??’,‘frog’,’?’,‘dog’,’??’,‘horse’,’??elephant’)”,
“Value”: “names”
},
{
“Command”: “storeEval”,
“Target”: “storedVars[‘names’].length”,
“Value”: “length”
},
{
“Command”: “store”,
“Target”: “2”,
“Value”: “num”
},
{
“Command”: “storeEval”,
“Target”: “storedVars[‘names’][${num}]”,
“Value”: “myrandomname”
},
{
“Command”: “echo”,
“Target”: “in array is ${myrandomname}”,
“Value”: “blue”
},
{
“Command”: “storeEval”,
“Target”: “storedVars[‘names’][${num}] = “Hello World!””,
“Value”: “myrandomname”
},
{
“Command”: “echo”,
“Target”: “in array is now ${myrandomname}”,
“Value”: “green”
},
{
“Command”: “storeEval”,
“Target”: “storedVars[‘names’][${num}]”,
“Value”: “myrandomname”
},
{
“Command”: “echo”,
“Target”: “in array is now ${myrandomname}”,
“Value”: “red”
}
]
}


#8

You are right - I see the issue now. To me, that looks like bug, and I made a ticket for it.

{
  "CreationDate": "2018-10-31",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed"
    },
    {
      "Command": "open",
      "Target": "https://a9t9.com/kantu/demo/storeeval",
      "Value": ""
    },
    {
      "Command": "storeEval",
      "Target": "new Array ('cat','dog','fish','dog','??','frog','?','dog','??','horse','??elephant')",
      "Value": "names"
    },
    {
      "Command": "store",
      "Target": "2",
      "Value": "num"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'][${num}]",
      "Value": "myrandomname"
    },
    {
      "Command": "echo",
      "Target": "in array is ${myrandomname}",
      "Value": "blue"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'][${num}] = \"Hello World!\"",
      "Value": "notused"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'][${num}] ",
      "Value": "newname"
    },
    {
      "Command": "echo",
      "Target": "in array is now ${newname}",
      "Value": "green"
    }
  ]
}

#9

We investigated the problem further.

By design, it is not possible in Kantu to update one element in an array in the way you and I tried in the test macro above.

The reason is that a statement like "storedVars['names'][${num}] = \"Hello World!\"" will be expanded to ["cat","dog","fish","dog","??","frog","?","dog","??","horse","??elephant"][2] = "Hello World!" during runtime.

So basically, all things in target of storeEval all happen in guest page context, variables in Kantu won’t be affected. And I think we should stick to this rule.

But we found a good solution (see next post).


#10

Solution:

You can achieve the goal of setting one element in an array by using the javascript array api function splice, and replace the whole array in kantu. And then assign it to the array again:

arr = storedVars['names'], arr.splice(${num}, 1, "Pink Elephant"), arr

If num = 2, this changes the 2nd element of the array from “dog” to “pink elephant”.

array1

Complete test macro:

{
  "Name": "replace element in how array",
  "CreationDate": "2018-12-9",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed"
    },
    {
      "Command": "open",
      "Target": "https://a9t9.com/kantu/demo/storeeval",
      "Value": ""
    },
    {
      "Command": "storeEval",
      "Target": "new Array ('cat','dog','fish','dog','??','frog','?','dog','??','horse','??elephant')",
      "Value": "names"
    },
    {
      "Command": "store",
      "Target": "2",
      "Value": "num"
    },
    {
      "Command": "comment",
      "Target": "change value 2 of the array from \"dog\" to \"pink elephant\"",
      "Value": "green"
    },
    {
      "Command": "storeEval",
      "Target": "arr = storedVars['names'],  arr.splice(${num}, 1, \"Pink Elephant\"), arr",
      "Value": "names"
    },
    {
      "Command": "storeEval",
      "Target": "storedVars['names'][${num}] ",
      "Value": "newname"
    },
    {
      "Command": "echo",
      "Target": "The ${num}nd element of the array is now ${newname}",
      "Value": "pink"
    }
  ]
}

String toLowercase?