Simple macro to check if variable exists within array?

Let's just say I have a macro to extract the numbers from a notification that I receive every couple minutes or so into a string, if it doesn't already exist within the array then it'll store the number, but if it does exist then it won't. Does someone have a macro I can go off of to learn how to accomplish this? There aren't many macro templates that use arrays and I couldn't find any that helped.
 

RSF

Well-known member
One option would be to set up an Action Block that your macro could call, in a single statement, to see if a value is present in an array:
Screenshot 2022-07-30 3.06.05 PM.png
Screenshot 2022-07-30 3.06.29 PM.png

You'd call it from your macro with the Action Block action, which allows you to specify parameters to send (in this case, the existing array and the value to search for), and to receive (a true/false value indicating if the value was in the array). That's the benefit of an Action Block vs. another macro. For example:
Screenshot 2022-07-30 3.22.33 PM.png

Your macro doesn't have to do any looping or searching; it can focus on populating the array, and doing whatever it needs to do if the value needs to be added (or is already there).
 

Dm114

Well-known member
Let's just say I have a macro to extract the numbers from a notification that I receive every couple minutes or so into a string, if it doesn't already exist within the array then it'll store the number, but if it does exist then it won't. Does someone have a macro I can go off of to learn how to accomplish this? There aren't many macro templates that use arrays and I couldn't find any that helped.
All depends on the number of different numbers (and their length) you are supposed to get in the period of time you monitor the notifications.

If their amount and length are not excessive (several thousands of characters) you could very simply concatenate them in a single string variable (separated by a space, a comma or any other character) and then, with a very simple If clause or a constraint, search whether the string already contains the new number (surrounded by the separator).

If you really want to use an array, it's a bit heavier (to write and at execution time as well as in space consumption) even if @RSF's solution is correct. But if you only use it in one macro you don't need to create an Action block: you could integrate these lines to your main macro.

¹: initiate the string with the chosen separator and then add every new number followed by this separator like this ,256,78,8732,732,
In this example, "732" existed, as well as "732," but ",732," didn't!
 
All depends on the number of different numbers (and their length) you are supposed to get in the period of time you monitor the notifications.

If their amount and length are not excessive (several thousands of characters) you could very simply concatenate them in a single string variable (separated by a space, a comma or any other character) and then, with a very simple If clause or a constraint, search whether the string already contains the new number (surrounded by the separator).

If you really want to use an array, it's a bit heavier (to write and at execution time as well as in space consumption) even if @RSF's solution is correct. But if you only use it in one macro you don't need to create an Action block: you could integrate these lines to your main macro.

¹: initiate the string with the chosen separator and then add every new number followed by this separator like this ,256,78,8732,732,
In this example, "732" existed, as well as "732," but ",732," didn't!
Ya I'm not too experienced with action blocks or this new array stuff, I'll probably take another go at it going off what RSF showed, and ya I prefer to integrate it into one macro since I'm not really gonna be using it for anything else, but the notifications always contain the 3 digits I need along with some other characters in it. I already got that covered though using text manipulation to extract them into a string variable, it's just the 2nd part where I need a simple way of storing it into an array, but checking if it already exists within that array. I'll see if I can figure out the method you suggested as well.
 
One option would be to set up an Action Block that your macro could call, in a single statement, to see if a value is present in an array:
View attachment 3114
View attachment 3115

You'd call it from your macro with the Action Block action, which allows you to specify parameters to send (in this case, the existing array and the value to search for), and to receive (a true/false value indicating if the value was in the array). That's the benefit of an Action Block vs. another macro. For example:
View attachment 3116

Your macro doesn't have to do any looping or searching; it can focus on populating the array, and doing whatever it needs to do if the value needs to be added (or is already there).
Cool looks simple enough I think, I'll integrate this and see how it turns out. Thanks.
 

Dm114

Well-known member
Ya I'm not too experienced with action blocks or this new array stuff, I'll probably take another go at it going off what RSF showed, and ya I prefer to integrate it into one macro since I'm not really gonna be using it for anything else, but the notifications always contain the 3 digits I need along with some other characters in it. I already got that covered though using text manipulation to extract them into a string variable, it's just the 2nd part where I need a simple way of storing it into an array, but checking if it already exists within that array. I'll see if I can figure out the method you suggested as well.
For numbers as short as 3 digits you don't need to use an array. A single string variable is enough.
 

Dm114

Well-known member
Then ya I just need to figure out how to do that, I'm guessing with the text manipulation thing too?
Use Text manipulation (Extract text) with:
- source text : the string variable containing the stored numbers (with separators, let's say ",")
- text to match : ,[lv=new_number], (tick First match)
- save to variable : another string variable
Then, if this string variable is empty (i.e. no match), add [lv=new_number] (with separator) to the string variable containing the stored numbers.
 
One option would be to set up an Action Block that your macro could call, in a single statement, to see if a value is present in an array:
View attachment 3114
View attachment 3115

You'd call it from your macro with the Action Block action, which allows you to specify parameters to send (in this case, the existing array and the value to search for), and to receive (a true/false value indicating if the value was in the array). That's the benefit of an Action Block vs. another macro. For example:
View attachment 3116

Your macro doesn't have to do any looping or searching; it can focus on populating the array, and doing whatever it needs to do if the value needs to be added (or is already there).
Just to update, I got this working so thanks, now I'm just wondering how battery consuming this would be when checking among thousands of entries in an array compared to @Dm114's method.
 
Use Text manipulation (Extract text) with:
- source text : the string variable containing the stored numbers (with separators, let's say ",")
- text to match : ,[lv=new_number], (tick First match)
- save to variable : another string variable
Then, if this string variable is empty (i.e. no match), add [lv=new_number] (with separator) to the string variable containing the stored numbers.
I found the solution which was a lot simpler than how I thought it was on my own -
Screenshot_20220731-190904_MacroDroid.jpg
It should now just be as simple as using an if statement to check if the second string contains the first string I assume, but thanks, I just wish people were less vague about it.
 
Top