I want to rearrange the order of the items to e.g. this: Orange, apple and pear.
The attached macro splits the sentence into new lines by comma and "and". It then asks for line numbers. To rearrange the above sentence, I want to be able to input "3,1,2" and have the items back together into a newly rearranged sentence.
Wondering how I can edit the attached macro to achieve this?
Once you've split the sentence into words, one per line, you can treat the variable as an array with \n as the item delimiter and access whichever item you want. So to get the 3rd word:
%Variable%SplitSentence[3]\n%
So "For Each" number in your input (eg 1,2,3) append the corresponding item to your output text, adding , , and or nothing, as appropriate.
I can see how the items can be joined back together like this:
%Variable%Local__SourceWithLneNumbers[3]\n%, %Variable%Local__SourceWithLneNumbers[1]\n% and %Variable%Local__SourceWithLneNumbers[2]\n%
But I cannot see anything in the 'For Each' action that will allow me to loop through each number in input. Any more pointers on how to use 'For Each'?
It is quite literally as the sentence above reads -- "For Each" through the numbers in your input (probably using the "substrings separated by: ," option) and in each loop you use an "Append Text to Variable" action. The "text" you append will be %Variable%SplitSentence[...]\n, putting the variable holding the number in the ... part of the array expression.
This is some next-level stuff, but there are plenty of examples on the Forum. See if you can get it working, but if you get stuck for more than 5 minutes -- shout!
Here's a single KM action that can do what you ask. Mind you, it only works when the "words" are at least 4 letters long each. Maybe you are okay with that. Actually, I might be able to remove that restriction also. I'm thinking about it.
Here's a way to do it that works even when some of the words are shorter than 4 letters long:
And only when there are three of them -- I know OP's example had three terms, but I don't know if that's guaranteed.
A bigger problem is that OP wants to split on commas and " and " while yours splits on word boundaries, including spaces and hyphens. That's more easily fixed, though.
I have two new, different solutions which work with any number of items. Here's the better one:
My other solution doesn't use shell commands, but is limited to nine words in a sentence. So it's simpler to understand, but not as complete of an answer. Here's the text if you want to use this solution:
sed 's/ and / \& /' | tr -c '[:alpha:]' '\n' | grep . | awk '{line[NR]=$0} END {printf "%s, ",line[NR]; for (i=1; i<NR-2; i++) printf "%s, ",line[i]; printf "%s and %s.",line[NR-2],line[NR-1]}'
Tried various ways of constructing For each action along the lines of attached. But I am still struggling to understand how to build the For each action. What variable should go in top line? What is a collection?
That the placeholder for "each thing in the collection, as we get to it". The collection is the set of things we're For Eaching through.
So for your input of 1,2,3 you'd use "substrings separated by ," to get a collection containing the substring 1, the substring 2, and the substring 3.
So on the first loop through the "For Each" above, the SentenceNewOrder variable would have the value 1, on the second loop 2, on the third 3 -- and there's no fourth because there are only three items in the collection.
Here's a bash at the macro you're after -- see if it makes more sense when you've got some context:
I've used variables for both the text (instead of the Clipboard) and the input (instead of a prompt) to make it easier to play around with.
If anything isn't clear, just ask. If it is clear -- consider improving it to cope with inputs with numbers greater than the number of items in the list.
Watch out for the final two Search and Replaces that deal with the last two , s, changing them to a "." and an " and ", respectively. It isn't obvious but they are set to "Last match" in their options -- can you see why, and why they're in that order?
I'm trying to provide a reply with a new solution. Hold on a second...
Okay I fixed up my approach. This time I used a hybrid approach: a little shell and a little KM. I could probably convert it to 100% KM or 100% shell fairly easily. But sometimes hybrid is okay.
Many thanks for help. I made some minor edits to macro attached. Mainly I edited it so it can deal with both 'and' as well as 'or'. This seems to work most times, but it has failed and incorrectly inserted 'or' instead of 'and' and vice versa.
You're testing for "and" or "or" -- the first will be true if the Clipboard contains "hands or feet", for example.
You can solve the problem most of the time by including the spaces in your search term -- %Space%and%Space% -- but you'll have to hope your text isn't something like "banana, fish and chips, toast or figs".
For completeness -- your regex (, )|( and )|(, )|( or ) is searching twice for "comma space". The "grouping" parentheses are only there to make what you are matching more obvious, so the action reads "replace any 'comma space' OR 'space and space' OR 'comma space' OR 'space or space' with a newline".