I built a subroutine that needs to pass back two variables while the subroutine action can only pass back one variable.
In reading, I have learnt that there are four ways of passing back multiple variables:
The use of instance variables.
The use of Jason strings.
The use of indexed arrays.
Concatenate the variables into one text string with a common delimiter — such as | —, pass back the concatenated variable and then separate using a RegEx.
I am curious as to what the best practice / preferred approach is which I understand may depend on the situation and use case.
I would appreciate an explanation as to the particular circumstances what the preferred approach is and why.
Totally data dependent and situational as to which is "best".
Instance variables save you from returning a combined value and "decoding" it in your main macro, but break a fundamental philosophy of subroutines -- independence. You need to use the same variable names in both main and sub which not only means you have to remember/find out the names to use but can, for subs you use across multiple macros, lead to misleading rather than descriptive variable names in your various mains.
JSON can solve many problems -- but adds the problem that you have to write, then read back, JSON
Pseudo arrays are easy but, as @evanfuchs points out, you need to be sure your data won't contain your array delimiter. Remember that the delimiter is a string, not a character, so you can use something like ==myKMSeperator==. If Local_myArray is:
cat==myKMSeperator==dog==myKMSeperator==fish
then:
%Variable%Local_myArray[2]==myKMSeperator==%
...gives you dog.
There's no point incurring the extra overhead of regex when you know the delimiter and that it won't also occur in your data -- that's what pseudo arrays are for.
I went with the Array approach and it worked great.
In trying to make things are easy (and as fast) as possible, I am in now in the habit of reading the WikI to get details of the action I want to use. It was clear in the manual that the delimiter is a string.
I wouldn't use my original version (which uses a subroutine as I use that bit elsewhere in other macros), but @noisneil's modified all-in-one version. You can easily add more Wiki sections to the pop-up menu just by adding them to the entries in the Prompt With List action.
I love having the Wiki page I need just a click or two away, and usable while I edit.
Great macro, I will make very good use of it. Just need to change the co-ordinates a but in make it better fit my screen.
I tried to reposition it using Moom. It responded to the Center command but not the Move Right command. Thinking it is because the window is a fixed size but will reposition things molar it work!
Will save it as a layout once I get the window properly sized to make it work.