I need help with the following task. I understand how to display delete the variables so you can see the lines of the variables which aren't used in the display text.
My Problem is that I use the Macro every day and every day and because I've set I=0 it always starts with the same URL then.
Anyone has an idea how I repeatedly start the macro every day and it always starts where it ended at the day before?
Do you also reset your variable to the entire URL list every time you run the macro, like you do in your example? That might be the actual problem. Otherwise, after you've deleted the first line from your Global variable is should stay deleted, and the next day you'll have a new first line.
Explain what you actually want to do. Do you want to use, then delete, the first line of a variable every time the macro runs? And what happens when you run out of lines? Or do you want to use the first line and then move it to the end, so you eventually cycle back to the beginning?
Where is this list of URLs coming from? Do you want to be able to edit it, add to it from within KM or from outside, use it in other places?
There are (at least) two different ways of achieving what you want -- keep a record of the last line used so you can go to the next line on the next run, or move/remove lines as you use them. Which you choose will depend on what you are doing.
I'm adding the URLs manually. When the list is almost finished I would add new URLs to the variable.
The URLs I used should be deleted completely.
At the moment I have to manually delete the used URLs in the variable and I just need a solution for the moment which deletes the used URLs automatically.
OK, so something like this. Set up a variable in KM's File->Settings->Variables pane called "Global_theURLList" and paste your list into it. Because it is a Global it will persist so each run of the macro will see the list the last one left behind after deleting the top line.
(You could also keep the list as a text file on your disk, updating it with a text editor, and using KM's "Read File" and "Write File" actions to read the data in and then save it out.)
I've been a bit lazy and done the "get and delete the first line" in one operation:
...where the ?s modifier at the start means "let . match new line characters as well". Then it is "From the start of the string Global_theURLList, match every character up to (but not including) the first new line and put that into Local_theURL. Then match everything after the first new line to the end and put that back into Global_theURLList".
The "Trim" removes any white space they may have been added around the URL when you added it to the list, just in case, then we open the URL in your default browser. The last few bits are just reminders if your list is getting short or is empty.
Just academically, if you wanted to get and delete the first, say, 10 lines of a list, would you repeat that process 10 times or is there a more efficient way you're aware of?
Previously, I've used a for-each with a counter.
While the counter is < 10, each line of var1 is appended to var2.
Once it's > 10, subsequent lines are appended to var3.
Do something with var2.
Finally, set var1 to var3.
Not very efficient for long lists, as you can imagine.
I think the "traditional" way would be "repeat x times, copying/appending line 1 to a variable then deleting it" -- so yes, you could use the above in a loop. In KM it might be more efficient to use a loop to grab the first x lines but do the deletion with an "Execute Shell Script" and tail -- you'd be balancing processing time within KM vs the "expense" of shelling, passing a large variable back and forth, etc.
But since I know you like your RegExs , how about this pattern?
(?s)^((?:[^\n]+\n){%Variable%Local_lines%})(.*)
Similar to the last, but matching the first Local_lines lines. Example:
I'd use something more likely to be unique than , as the output separator -- unless you are sure your input won't contain any commas (and I'd still add a "test and error" section for when I forgot that limitation).
I'd stayed away from them because making sure we use the same instance names in our main and sub isn't in the spirit of sub-routines. But how about if we pass in the names of the instance variables as parameters, maintaining the "blackbox-iness" of a proper sub-routine?
Ooh. Can't decide which one I like best. Comes down to which one's easier to recall how to use in the moment. I'll keep both and see how it goes. Thanks!