So I was tooling around with KM, as you do, and wondered about reversing the characters in a string. Most languages have a built-in to do that, KM might too (I never looked!), but if a language has a particular feature you can always go back to basics and DIY...
Here's an example in AppleScript:
reverseString("Hello World!")
on reverseString(aString)
--display dialog "Chopping -- string is \"" & aString & "\""
if length of aString > 1 then
set firstChar to first character of aString
set aString to reverseString(characters 2 thru -1 of aString as text)
else
return aString
end if
set aString to aString & firstChar
--display dialog "Joining -- string is \"" & aString & "\""
return aString
end reverseString
If you aren't sure what's happening, remove the comment marks ("--") from the two "display dialog" lines so you can "step through" and see how "aString" changes.
But can this be replicated in KM? Turns out, it can!
String Reverse.kmmacros (6.7 KB)
String Reverse Sub.kmmacros (4.6 KB)
Run "String Reverse" and it'll use "String Reverse Sub" as a subroutine to do the actual processing. Which is either brilliant, or has just earnt me the Forum's "Complete Idiot" badge and a lifetime ban
Biggest gotcha, as stated in the opening dialog, is the limit on the length of string you can process. Entering 25 or more characters fails without notification, crashing the KME. Coincidentally, I think it's related to this current topic, except every subroutine instances counts as two "normal" macros (tested by running increasing numbers of "stay open" macros -- every 2 decreased the number of characters that could be process by 1).
So (if you've got this far) -- KM does recursion! I'm not sure how useful that actually is in practice -- perhaps tree-walking to process folders of folders of files? -- but some might find a use for it.
Note: I did have a quick search through the Forum, but the hits I found for "recurs" were mainly JavaScripts etc rather than KM-native. Apologies if this feature is well-known and has been done to death already...