Get value from large array of information. Macro needs speed optimization

I have an array of about 2400 pair of items formatted as "1234,2"
I want to find out what 2nd number does the passed variable correspond to.

Eg. If I pass 1234 to the macro, I should get 2 as the response.

I currently have this: (Works but is slow)

Used @JMichaelTX's macro example: Set Variable Y Based on Range of Variable X @Example as a starting point.

Have you tried simple RegEx? It generally works very fast.
Here's an example with 57 lines that runs very fast.
Try it with you 2400 line array. Just change the RegEx to Search a file instead of a variable.


Example Output

image


MACRO:   Extract Number from Text Given Keyword [Example] @Regex


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/f/7/f773438b36b1a89c3f06d6be9c61b37ac60f28d2.kmmacros">Extract Number from Text Given Keyword [Example] @Regex.kmmacros</a> (8.8 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### ReleaseNotes

Author.@JMichaelTX

**PURPOSE:**

* **Extract Number for given Keyword**

**NOTICE: This macro/script is just an _Example_**

* It is provided only for _educational purposes_, and may not be suitable for any specific purpose.
* It has had very limited testing.
* You need to test further before using in a production environment.
* It does not have extensive error checking/handling.
* It may not be complete.  It is provided as an example to show you one approach to solving a problem.

**REQUIRES:**

1. **KM 8.0.2+**
  * But it can be written in KM 7.3.1+
  * It is KM8 specific just because some of the Actions have changed to make things simpler, but equivalent Actions are available in KM 7.3.1.
.
2. **macOS 10.11.6 (El Capitan)**
  * KM 8 Requires Yosemite or later, so this macro will probably run on Yosemite, but I make no guarantees.  :wink: 


**MACRO SETUP**

* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  😉
.
* Assign a Trigger to this maro.
* Move this macro to a Macro Group that is only Active when you need this Macro.
* ENABLE this Macro.
.
* **REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:**
  * ALL Actions that are shown in the magenta color

**USE AT YOUR OWN RISK**

* While I have given this limited testing, and to the best of my knowledge it will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
  * **Ask first**
  * Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

---

![image|496x1587](upload://6wsYwn4INFoXhP4p5ByWZBaGKIV.jpeg)

---

### Let us know if this works for you, or you have questions?

That's why I love this community!
Thanks a lot @JMichaelTX. That was insanely fast.
Right now I am just searching from Variable array. Is there any advantage to using a Txt file instead of Variable?

I mean I am pretty impressed with the speed already. It's insanely fast!

Thanks again.

@JMichaelTX Just found out that some of my QID have multiple pages mentioned.

Is it possible to show all matches?

For eg. 11966 has 3 different mentions (11966,10) (11966, 13) (11966, 180)

Certainly. As one possible way to do this, we can tweak @JMichaelTX's example macro to use the For Each action to collect each matching keyword number into one results variable:

Extract Multiple Numbers from Text Given Keyword [Example].kmmacros (5.3 KB)
image

14%20PM

To use this macro with a text file instead of a variable (either should work fine, but I imagine it would be more convenient to use a text file if that's what the array is already in) just change this option in the For Each action:

16%20PM

to this:

17%20PM

and point it to the file in question.

1 Like

Nice job Gabe. Thanks for jumping in.

1 Like

Gabe, I didn't notice until after my above post, but you also changed my RegEx pattern:

image

Mine is:
(?mi)^%Variable%Local__KeyWord%,(\d+)

The key difference is that mine requires that the keyword start at the beginning of the line. I haven't tested yours, but I believe it could yield a false positive if the keyword was partially contained in another line, as in:

123456,12
456,9

I believe your RegEx would find the first line for a keyword of "456".
I'll leave it up to you to test and confirm/refute my assertion. :wink:

1 Like

Thanks Jim, and good catch! You're right, this was an oversight in my suggested macro. The correct regex needed here should be

(?m)(?<=^%Variable%Local__Keyword%,)\h*\d+

essentially restoring the multi-line flag and start-of-line marker you included in your macro's regex.

@JMichaelTX @gglick Thanks a lot. Works like a charm!
Insanely fast. :slight_smile:

1 Like