Seemingly Simple Replacement Task That Has Me Stumped

I have two lists stored in variables, one of which is in a specific order and has section headers. Let's call this one var_Main:

Group One
??•Foo1•details
??•Bar2•details
??•Foo3•details
??•Baz1•details
??•Bin0•details

Group Two
??•Bin1•details
??•Baz7•details
??•Foo6•details

I use this list, including the header rows (Group One, etc.) to populate a Prompt With List action. So the order is important, and cannot be changed.

The second variable is always a subset of the main list, but it has values in the first field. Call this var_Subset:

f1•Foo1•details
ba1•Baz1•details
f6•Foo6•details
bi1•Bin1•details

The first field (f1, etc.) is guaranteed to be unique, and it (and the "details" bit) will always match something in var_Main.

What I need to do: For each line in var_Subset, I need the first field inserted into the matching row in var_Main, replacing the ??. If the order of var_Main didn't matter, I'd be fine—it's not trivial (for me), but I had it working in one version.

But I'm stuck trying to figure out how to do this while maintaining the order. I can find matches easily, but after that, I'm unsure how to say "take the first bit from this particular match in var_Subset and insert it at the first field of the corresponding line in var_Main.

It almost seems like I need two nested "for each" loops, but my first attempt at that was something of a disaster.

Any ideas?

-rob.

The answer was indeed two "for each" loops, one inside the other. Basically, for each row in var_Main that matches a criteria I set, I then loop through each row in var_Subset looking for the exact match. Once I have that, I can do a find/replace back into var_Main without messing up the sort order:

Sample project attached, in case anyone's curious.

Macro Screenshot

Dual-group replacement test.kmmacros (8.3 KB)

(My actual macro is more complicated, of course, because I have to generate the local_Subset and local_SubsetFull variables from scratch.)

-rob.

Hey Rob,

If I understand your task correctly I think you've overcomplicated it a bit.

I've pared it down to one FOR loop.

-Chris


Dual-group replacement.ccs v1.00.kmmacros (9.0 KB)

Macro-Image

Simpler would definitely be better :). Thanks, I'll take a look at this tomorrow and try to munge it into the real macro to see if I can make it work.

-rob.

1 Like

Yep, that worked perfectly, and is much simpler. In the end, the bit that had me going to two loops was this relatively simple part of the regex:

([^•\n]+)

I couldn't figure out how to capture the line with the ?? knowing I had letters in the to-be-matched string instead of ??.

I did have to make one modification, and that was my fault, not yours: For the sake of an easy demonstration, I used a very simplified data set in my post. In the real data, the "details" part is a full URL, so I added one step and changed the next:
Keyboard Maestro Export
Without filtering, the regex search will fail, because there are lots of protected characters in the URLs.

But now it works great, and it's 4x faster than my dual-loop method—thanks again!

-rob.

1 Like