Suggestion for a New Collection in the For Each Action: List Items

I have a list with a custom separator. I want to do something with each item in the list. Similar to the collections for Dictionary Keys and JSON Keys I'd like a List Values collection.

I know it's already possible, but (unless I'm just flat missing something, which is very possible) I usually use a number range as an index counter and extract each item from the list, but using %Variable%ListName[index]Custom_Sep% is prone to typo errors compared to %Variable%Local_ListItem%

The UI I'm thinking of is similar to the substrings collection, except instead of a regex you just put the separator character (default to ,). Ignoring case goes, variable is the List Variable, as string goes.

I agree typo's are likely in that scenario.

Here's a possible workaround until your feature request is implemented:

What I usually do in this case, is to use the KM Search and Replace action to replace the "Custom_Sep" character with a LF, and save to a temp KM Variable.

Then use the For Each with a Lines Collection with this temp variable.

Having said all that, I completely agree with your request. It would be very useful.

Ah, that's a good idea unfortunately the list items for the situation that made me think of this may contain LF characters in them.

In fact my custom separator is actually :capital_abcd:, which is in the running to be the least used emoji, just because the contents of the items can contain , or every other ASCII character I thought of using (I usually use tab)

I have to admit that I had my doubts about where a KM Variable Array could contain LFs. But my simple test proves they can:


Hey Kevin,

Given this is possible now I'd be a bit surprised if @peternlewis would devote precious development time and effort into such a feature.

Working with Delimited Text v1.00.kmmacros (7.1 KB)

However โ€“ it's not a bad idea. Most programming languages have some sort of split-string function and the ability to save the result to an array.

Perl for example:

Working with Delimited Text (Perl) v1.00.kmmacros (5.9 KB)

And โ€“ it seems to me the workload wouldn't be very high โ€“ Peter could leverage the current SubStrings Collection and just add With Delimiter to the existing UI.

Keep in mind that if you want Peter to see something on the forum you need to ping his user-name to get his attention as I've done above. He's much too busy to read all the traffic anymore.

Also โ€“ if you really want the feature then make your case โ€“ why is this feature worth his time? What kind of use-cases make it a main-stream feature?



1 Like

Your regex is not a generic array item extractor. It only works for me when the separator is followed by a LF, which is more along the lines of what @JMichaelTX was suggesting. It doesn't work with lists that are just, like in most of the examples in the wiki, comma separated value lists.

The use case for my proposal is any time you want to loop over a list of items connected with a separating character. The reason I think it's better is that it is significantly easier to use, especially for one of KM's target markets of non-programmers.

Writing a new regex every time you want to do this and your list contents might be different than you've previously done -- sucks. Using a bunch of actions to support extracting by the number range, sucks. Replacing the separator with a carriage return & using the By Line for-each construct is probably the simplest and works, I expect, in the majority of cases, but even that is complicated by needing extra actions to change the list separator.

My proposal does it all in a single action that involves no modification to the list, supports all constructs where item values are anything, except the list separator and any separator that doesn't appear in the item value. The current way of extracting item values 1 at a time doesn't support escaping the separator character, and this should follow the same rule.

Here's an example macro with the different test scenarios I came up:

Macro Download:

Array List Separator Tests.kmmacros (10.3 KB)

You're talking about my macro in post #5?

All you have to do to change that is remove the beginning-of-line anchor in the pattern...

(?ms)[^๐Ÿ” \z]+

For that matter you can put the delimiter in a variable to make changing it simpler for the user.

Working with Delimited Text v2.00.kmmacros (7.4 KB)

Also โ€“ take note of the fact that Peter has now noted this thread.


I have added โ€œseparated byโ€ options to the Substrings In collection for the next major version.