I am sure the answer is very simple, but how do you do the following:-
I have an array with values
e.g. testarray is 100,200,300,400
I just want to replace 300 with 600
So I would say
ix = 3
testarray[ix] becomes 600
How does the logic change if my test array is characters
e.g. testarray2 is ONE,TWO,THREE
and I want to change TWO to SIX.
so ix = 2
so testarray[ix] = SIX
I need to use ix, and not absolute index values e.g. 3,2, which work OK.
Finally can an array hold characters as well as numbers eg
I don't know what journey you are headed on, but if it requires much array manipulation, then I would immediately abandon array handling in KM, and use a tool like JXA (JavaScript), which has very powerful and easy to use array functions.
To be clear: KM does NOT have true arrays.
ALL KM variables are TEXT.
It has pseudo array handling, but it is very limited and mainly intended to get numeric values (stored as text) out of the "array".
To use KM Variable "arrays" like you are asking about will require the following:
Use of dynamic Variable names, with Process tokens filter
Use of complex Regex to do the replacement
The following Macro demonstrates the use of these.
I did this only to see if I could do it. Having done it, I personally would NOT use it in production.
Again, I highly recommend JXA for this task.
Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.
I started to write a KM Sub-Macro to do this, and it just requires too many Actions.
So I wrote a simple JXA script instead. You can use this script anywhere you want to replace the KM array element. Just need to set these two KM Variables as input:
REQUIRES These KM Variables as Input:
• Local__Array -- KM Pseudo array, comma-deliminted
• Local__ReplaceParams -- IndexToReplace, NewValue
Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.
There is no direct way to do this in Keyboard Maestro, so there are only two indirect ways:
Break the string in to three parts (before),(element),(after) and then put it back together (before),(newvalue),(after).
Iterate through all the elements, and use the current value unless it is the desired index in which case use the desired new value.
The first case requires some special casing for the start and end values, so the second case is probably easier. More special casing will be required if you want the ability to index outside the bounds of the existing array.