I want to count the number of commas in a string, so the plan is:
Get length of string
Remove commas from string
Get length of updated string
Calculate difference of both lengths
But I'm stuck at step 2. The string Remove command only removes the first instance of the chosen character. Interestingly, the Replace command will work on all instances of the character.
Is this a bug? I've attached a test rule that shows this issue.
I initially treated RM as a programming/scripting language and quickly learned that too much complexity has performance and management issues. So now I try to keep it as simple as possible or just compromise with hardcoding some values.
'Repeat While...' should work but doesn't.
'Replace' and 'Repeat' should behave similarly, but they don't.
Compounding that with my own recent issues/experience leads me to conclude there are quite a few string manipulation/interpolation issues in RM right now.
Get a random number between 0 and delimiter count.
Use delimiter and random number in token action to choose random value from string.
Further down the rabbit hole:
String is a delimited list of dog barking mp3 filenames stored on my nas.
Via MediaRenderer app, I send a url to the chosen mp3 file to a DLNA speaker (eufy genie) to play.
Wyze camera detects person and triggers alexa routine via wyze alexa skill. Routine triggers a hubitat virtual switch via hubitat alexa skill. Hubitat rule triggers upon virtual switch to play barking sound file.
Reason I asked is, if you know the number of delimited items, it's trivial to pull individual elements from a list in Rule Machine, using Token.
Now that I see you're contemplating using this technique with filenames, then things get even easier: Just name the files bark1, bark2, etc. and after letting RM pick a random integer, compose the filename with "bark%num%.mp3".
Also, my initial implementation has a hardcoding of the item count, but I wanted to try a more dynamic way. So now I have the list of filenames in a hub variable. And there is another hub variable with the count of filename delimiters in the string, which is updated by a rule trigger when the filenames variable is changed.