Compare two strings [Solved]

Snurre

Well-known member
Hey all
I've got two string variables, old and new
I would like to compare these and if they are not equal (removed/added text), save the difference in the var diff
I'm a little lost here, is it possible ?
Nice day to All ๐Ÿ™‚
 
Last edited:

Snurre

Well-known member
Shure
Some apps "forget" accessibility services sometimes, so I have set old var to enabled accessibility services and then run a macro in certain intervals that set the services in new var
I would then like to check for differences
Hope this makes sence
 

dsnz

Well-known member
Shure
Some apps "forget" accessibility services sometimes, so I have set old var to enabled accessibility services and then run a macro in certain intervals that set the services in new var
I would then like to check for differences
Hope this makes sence
from what you say one understands that you only care if there is a difference and not the exact difference of the strings (which is a complex thing to show, eg. for strings abc and acef one way to specify the difference would be d2..2a3..4af ie. delete characters in range 2..2 then add characters af in range 3..4)
so .. what exactly are you interested in ? (I find it strange that Snurre would ask such a trivial thing ๐Ÿ˜‚)
 

Snurre

Well-known member
Yeah I know, I'm a little bit confused at the moment ๐Ÿ˜‰
But ok, I want to find out the exact difference in the two variables
In the old var there is different apps listed in one long string, in the new var I want to check if there's added/removed some apps
That's my problem ๐Ÿค”
 

Snurre

Well-known member
probably he wants two arrays or strings as result

deleted: app2 app5
added: app4

right @Snurre ?
Yeah I think thats what I want
My old var looks like this

com.arlosoft.macrodroid/com.arlosoft.macrodroid.triggers.services.MacroDroidAccessibilityServiceJellyBean:com.arlosoft.macrodroid/com.arlosoft.macrodroid.triggers.services.VolumeButtonAccessibilityService:com.tribalfs.gmh/com.tribalfs.gmh.GalaxyMaxHzAccess:eek:rg.de_studio.recentappswitcher.trial/org.de_studio.recentappswitcher.service.MyAccessibilityService:apps.ijp.coveros/apps.ijp.coveros.CoverOSSupportEngine:com.arlosoft.macrodroid/com.arlosoft.macrodroid.action.services.UIInteractionAccessibilityService:com.arlosoft.macrodroid/com.arlosoft.macrodroid.triggers.services.FingerprintAccessibilityService

So now it's "just" to make the comparison with a new var and save the result, as mentioned, I'm blank ๐Ÿค”

It's not only the app name it safes but also accessibility name, it's a bit tricky for me to figure out
 
Last edited:

Snurre

Well-known member
Found a little workaround

When I compare my two var's with 'if/else' I made a popup with "No change" if they are equal, else "Changed"
If there is a change I run another macro that opens an app called "DiffFinder 2" and with some UI actions copy/paste the two var's in this app and compare them

It actually works ๐Ÿ˜Š

Still trying to figure out a more elegant solution, I am not a big fan of UI actions

Thanks for your time guys
 

dsnz

Well-known member
Found a little workaround

When I compare my two var's with 'if/else' I made a popup with "No change" if they are equal, else "Changed"
If there is a change I run another macro that opens an app called "DiffFinder 2" and with some UI actions copy/paste the two var's in this app and compare them

It actually works ๐Ÿ˜Š

Still trying to figure out a more elegant solution, I am not a big fan of UI actions

Thanks for your time guys
for a true MD solution that does not need many 100s of lines of code
@MacroDroidDev must add some functions/actions to work with arrays/dictionaries
eg. split string into array (though @FrameXX has indeed posted a split action block, this should be inbuilt in MD), insert an array element, delete an array element, join elements of an array into a string, sort array elements .. and some more (as can be found in other languages like Python, Java, etc.)
some patience

for your case at the moment a thought about free public web api that compares strings, it's better than UI actions
 

Snurre

Well-known member
for your case at the moment a thought about free public web api that compares strings, it's better than UI actions
You are absolutly right, I have spent a lot of time to find some, but no luck yet
 

Snurre

Well-known member
Right now I'm fooling around with thoughts about using MD's 'shell script', I have written the var's to two files and will try to use 'diff' and see if I can get this to work ๐Ÿคž
 

Dm114

Well-known member
Right now I'm fooling around with thoughts about using MD's 'shell script', I have written the var's to two files and will try to use 'diff' and see if I can get this to work ๐Ÿคž
New array/dictionary feature would be perfect for you but you would need to sort both old and current lists and then compare each item.

The principle is quite simple. The harder will be to create a sort routine. Classical job for "computer science beginners"... ๐Ÿ˜‰
 

Snurre

Well-known member
Aaarhh, the diff works, but but but I'm getting nada out of it๐Ÿ‘Ž
The problem is that the settings is one long string with all the services and appnames, so the diff tells me there is one change in both files (I've made a test change in one file) but the output looks like this

--- /storage/emulated/0/Documents/workingaccess.txt 2022-05-02 12:30:12.471444652 +0200+++ /storage/emulated/0/Documents/currentaccess.txt 2022-05-02 14:35:15.000000000 +0200@@ -1 +1 @@-com.arlosoft.macrodroid/com.arlosoft.macrodroid.triggers.services.MacroDroidAccessibilityServiceJellyBean:com.arlosoft.macrodroid/com.arlosoft.macrodroid.triggers.services.VolumeButtonAccessibilityService:com.tribalfs.gmh/com.tribalfs.gmh.GalaxyMaxHzAccess:eek:rg.de_studio.recentappswitcher.trial/org.de_studio.recentappswitcher.service.MyAccessibilityService:apps.ijp.coveros/apps.ijp.coveros.CoverOSSupportEngine:com.arlosoft.macrodroid/com.arlosoft.macrodroid.action.services.UIInteractionAccessibilityService:com.arlosoft.macrodroid/com.arlosoft.macrodroid.triggers.services.FingerprintAccessibilityService\ No newline at end of file+com.arlosoft.macrodroid/com.arlosoft.macrodroid.triggers.services.MacroDroidAccessibilityServiceJellyBean:com.arlosoft.macrodroid/com.arlosoft.macrodroid.triggers.services.VolumeButtonAccessibilityService:com.tribalfs.gmh/com.tribalfs.gmh.GalaxyMaxHzAccess:eek:rg.de_studio.recentappswitcher.trial/org.de_studio.recentappswitcher.service.MyAccessibilityService:apps.ijp.coveros/apps.ijp.coveros.CoverOSSupportEngine:com.arlosoft.macrodroid/com.arlosoft.macrodroid.action.services.UIInteractionAccessibilityService:com.arlosoft.macrodroid/com.arlosoft.macrodroid.triggers.services.FingerprintAccessibility\ No newline at end of file

That means I still have to read it All, so nothing gained here โ˜น๏ธ

As @Dm114 said
The principle is quite simple. The harder will be to create a sort routine. Classical job for "computer science beginners"...

But when it's one long string with letters, numbers, Capital letters, signs and so on among each other with no specific pattern, I just say good luck sorting this ๐Ÿคฎ
Still trying to find a webservice with this function
 

dsnz

Well-known member
@Snurre
your strings should have all services separated by a single character,
preferably a \n (new line) so you can see nicely the services line by line
then you do the following

1.write the strings to files old.txt and new.txt
2. using shell script(s) you do

sort old.txt > old_sorted.txt
sort new.txt > new_sorted.txt
comm -23 old_sorted.txt new_sorted.txt > deleted.txt
comm -13 old_sorted.txt new_sorted.txt > added.txt

and finally you have in

deleted.txt = services deleted
added.txt = services added

problem solved ๐Ÿ˜Š
 

Snurre

Well-known member
@Snurre
your strings should have all services separated by a single character,
preferably a \n (new line) so you can see nicely the services line by line
then you do the following

1.write the strings to files old.txt and new.txt
2. using shell script(s) you do

sort old.txt > old_sorted.txt
sort new.txt > new_sorted.txt
comm -23 old_sorted.txt new_sorted.txt > deleted.txt
comm -13 old_sorted.txt new_sorted.txt > added.txt

and finally you have in

deleted.txt = services deleted
added.txt = services added

problem solved ๐Ÿ˜Š
I get your point, it sounds ok ๐Ÿ‘
BUT then I still have to manually edit the files with new line, before I can use it in scripts โ˜น๏ธ
 

Dm114

Well-known member
Yeah but I can't see how in this paticular issue
Try to have a look at the string in post #8 and pls give me a hint ๐Ÿ˜‰๐Ÿค”
It's quite impossible to compare anything this way. Why don't you have any new lines? How do you get these string variables?
 
Top