Retrieving contents of an increasing variable

Hi there,

Im finding myself stuck retrieving content from variables. I have a loop which puts a content into the variable "adrin" plus an increasing variable "adrnum" So together the variables are adrin1, adrin2, adrin3

I start with the macro by setting adrnum to 1...

When the macro finds what I am looking for (in the photos below %Variable%TestMatch% is retrieving the correct information) it stores the value I need to the variables adrin1, adrin2, adrin3 etc. then kicks up the variable %adrnum% by one to to start the loop over and log the next one.

These values are being logged correctly into the increasing variables as seen by my Display Text


I would like to put the content of these variables back into the clipboard one at a time to run actions on each one. However when I try to recall the variables Im not getting the content of the variable correctly. So if this was done correctly the number contents in my display box above (like 02:07:57:14 etc) would be put back into the variable adrputback in the picture below. Instead it is assigning it as just 'adrin1' 'adrin2' and not the content i want back in the clipboard one by one.

Thank you for the help!

Rather than store each item from your source in a separate KM Variable, another approach is to store all items in one KM variable, one item per line. I find this makes it easier to add and process items.

So here is a pair of macros to illustrate this approach.
Please feel free to ask any follow-up questions.
If you don't think this approach will work, then post back here and we'll try to help yo with your current approach.

How To Use These Macros

  1. Use the first macro, Add Items to List [Example], to add any text you select to the list as an item in the list.
  • You will see a notification after each item is added to the list
  • At any time you can see the list by viewing the KM Preferences > Variables panel, and searching for "DND_PIL__ItemList" (the "DND" stands for Do Not Delete).
    .
  1. When you're ready to process the items, run the 2nd macro, Process Items in A List [Example]
  • Instead of putting the items back on the Clipboard to process, I put them in a Local variable in a For Each loop.
  • You will see a notification as each item is processed (nothing more than just the notification for now. Of course this is where you will add Actions that you want.
  • After the loop completes, you have an option to clear the list.

##Macro Library #1:   Add Items to List [Example]


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/a/a3222949dfbee55e30b3cf8298d749ccd82cf9f6.kmmacros">Add Items to List [Example].kmmacros</a> (2.1 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---


<img src="/uploads/default/original/2X/8/8e86accbabb052e79267c146026db2c67a0125f8.png" width="459" height="600">

##Macro Library #2:   Process Items in A List [Example]


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/9/936980304edab07042cacf974bb86c18a2826d5c.kmmacros">Process Items in A List [Example].kmmacros</a> (7.0 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---


<img src="/uploads/default/original/2X/6/62ede731de6c5c87aa05feb15bda69fcade3d22d.png" width="488" height="1695">
1 Like

@JMichaelTX is right that just using one variable is probably a better solution. Or in this case, you could also use a dictionary.

To answer your question, you are setting adrputback to the name of the desired variable, which is fine, but you want the value of that variable. There is a Filter for example that purpose:

Or you can do it in a single action:

2 Likes

If you're running KM8, you can use the new variables collection in a For Each loop to automatically grab the value of each adrin variable. One advantage this method has over @JMichaelTX's solution is that you wouldn't need to change your current method of setting variables, as you could just add this to the appropriate section in your already-working macro.

##Example
[Example] Auto-Retrieve Variable Contents.kmmacros (7.1 KB)

##Results

1 Like

@BillytheHicks, yep that is another approach. The issue I have with using variables to build other variables is the extra confusion and Actions. IMO, these are not obvious or intuitive, but they do work. Sometimes when things aren't working, it is an opportunity to revisit your method, and make any adjustments that are needed.

I always strive for simplicity -- easier to write and easier to understand, usually (but not always).

With my approach, the single-variable, multi-item approach, you have these advantages:

  1. One KM variable that is easy to find and use to view all of your items.
  2. View your item list anytime in the Preferences > Variables just by selecting one variable.
  3. Clear all your items in one Action.
  4. If you need to operate on the entire list, say with RegEx to change the data, it is easy.

So, you have a couple of choices on how to model your workflow -- that is good! Choose the one that works best for you.

3 Likes

Well said. There’s really no one right approach here; in my experience, it all comes down to knowing what options you have available, and finding the ones that best fit the way your brain naturally works. For some people, processing individual lines on lists can be less intuitive than processing individual variables even if the contents of each are identical, and of course for others it’s the opposite. For my part, I think the new variables collection in KM8 is a great new option that we didn’t have before, so when the chance presents itself I think it’s worth trying to show others how it can be useful (as well as trying to train myself to remember it’s now an option for my own uses :slightly_smiling_face:)

2 Likes

First off thank you everyone for your suggestions. I thought I would go in sequential order and Michael yours worked perfectly for what I needed.

[[quote="gglick, post:6, topic:8329"]
it all comes down to knowing what options you have available, and finding the ones that best fit the way your brain naturally works
[/quote]

Exactly, Ill be going through the others today too so that I can understand more of what KM can do and the different ways of achieving things. Also hopefully others will find this thread and see the different options and what can work for them.

You couldn't be more right on this. Ive been transitioning scripts I built on PC to Mac recently. The method I was trying was how I had built it on PC.

2 Likes