Jump to content
Macro Express Forums

Dereferencing a variable


ThatJimGuy

Recommended Posts

OK, here's what I want to do: set string variables (T1, T2, T3, etc.) then run a macro that has a repeat counter, the current value of the counter being the x value of Tx. For example, first loop pass, I want to do something with T1, next loop, T2, etc.

 

I would really like to dereference the variable, instead of using a "case" statement, like thus (which doesn't work, btw, and I have also tried to "run macro in a variable, which also doesn't work):

 

Variable Set Integer %N1% to 1

Variable Set String %T1% "whatever"

Variable Modify Integer: Convert %N1% to text string %T2%

Variable Set String %T3% "%T%T2%%"

// But I want T3="whatever" NOT T3="%T%T2%%" !!!

 

 

PS This is simplified.

 

Any ideas? Remember, I want it simple and fast, no I/O.

 

tia,

 

-jim

Edited by ThatJimGuy
Link to comment
Share on other sites

Click Options, Preferences, Miscellaneous, Advanced and change the Variables Evaluation Level to 2 or higher. Then use this macro:

 

Variable Set Integer %N1% to 1
Variable Set String %T1% "whatever"
Variable Set String %T3% "%T%N1%%"

For more information see this web page: Macro Express News - July 13, 2005 and these posts: Increment variable name, How to increment variable name, Need Clarification on Incrementing Text Variables, T1 becomes T2 etc. on each repeat. How?, Nested Variable?, How do I make a variable variable?, Setting Variable %t%n1%%

Link to comment
Share on other sites

  • 1 year later...
Click Options, Preferences, Miscellaneous, Advanced and change the Variables Evaluation Level to 2 or higher. Then use this macro:

 

Variable Set Integer %N1% to 1
Variable Set String %T1% "whatever"
Variable Set String %T3% "%T%N1%%"

 

Thanks Kevin (2 years later lol) that works for text, but I cannot for the life of me figure out how to get it to work for integers. I want to set N[N9] to N10, but when I use the "Variable Set Integer" command, it will not accept %N%N9%% as a variable to set. If I try to set T4 to a variable with the dereference (Variable Set String %T4% "%N%N9%%") , T4 always ends up to be "0", no matter what N9 is (and N9 <> 0!).

 

If I try to do it with the direct editor (<IVAR2:%N%N9%%:01:%N10%>), ME crashes. So does setting T5 to "<IVAR2:%N%N9%%:01:%N10%>" and using the "Run Macro in Variable %T5%" command. No, it doesn't like this at all lol ;-) It doesn't like this either (thinking that maybe I had to delimit the % symbols): <IVAR2:\%N\%N9\%\%:01:\%N10\%>

 

So I guess I'm back to a case statement based on the counter in the repeat loop unless you have any ideas on dereferencing integers (or decimals)?

 

If my post doesn't make sense, please let me know.

Basically, I want to set %N%Nx%% to %Ny%, where x is variable and y is static.

Link to comment
Share on other sites

I think you might be looking for a more complex solution than is strictly needed....

 

I want to set N[N9] to N10

 

This is actually a lot easier to do than your post was moving towards. If you want to set %N9% to %N10%, you simply need to use the Variable Set Integer command to accomplish this:

 

Variable Set Integer %N9% to %N10%

 

<IVAR2:09:01:%N10%>[b]

Link to comment
Share on other sites

I think you might be looking for a more complex solution than is strictly needed....

 

 

 

This is actually a lot easier to do than your post was moving towards. If you want to set %N9% to %N10%, you simply need to use the Variable Set Integer command to accomplish this:

 

Variable Set Integer %N9% to %N10%

 

<IVAR2:09:01:%N10%>[b]

 

Sorry, I was very tired when I wrote that.

 

N9 contains the NUMBER of the N variable I want to set.

So, for instance, let's say I have 5 integers (N1, N2, N3, N4, N5) I want to set to a random number. I have a repeat loop, which repeats 5 times, putting the counter into N9.

 

Here is what I want to do:

 

Repeat 5 times (put counter into N9)

Set variable N%N9% to some calculated value

End repeat

 

So the first loop, it sets N1, the second, it sets N2, and so on until pass 5 where it sets N5.

 

If I try to set %N%N9%% in a "Variable Set Integer" command, I get this error:

NN9.jpg

 

So, I finally solved it by doing this, in case anyone else out there ever tries to do the same thing:

 

Repeat Start (Repeat 5 times)

Variable Set Integer %N10% with a Random Number

Variable Set String %T4% "<IVAR2:%N9%:01:%%N10%%>"

Run Macro in Variable %T4%

Repeat End

 

NOTE the single % signs around N9 (this is the VALUE of N9, i.e. 1,2,3...) and the double % signs around N10 (if you put just single % signs around it, you would be trying to set %N9% to %N%N10%%!!)

 

That's a lot of gobbledygook so just trust me, if you don't do it this way, you will be setting N9 to things like N65, N33, or whatever the random number is; you will be setting N9 to random variables, not the random number that is in N10.

 

Also, don't even try to put "<IVAR2:%N9%:01:%%N10%%>" in the direct editor and then switch back to the scripting editor (well go ahead and try it if you want, but don't hold me responsible when your macro looks funky after) ;-)

 

It would be nice to be able to just put %N%N9%% in the "Variable Set Integer" command and the equivalent for the decimals. The set text commands allow for it, but you have to use the direct editor equivalent and run it in a macro to set integer and decimal values like this.

 

TMI? Probably. Not good to program and post when you're sleep-deprived, so if this doesn't make sense or parts are incorrect (don't worry, the CODE is correct), let me know and I'll edit it tomorrow after a good night's sleep.

Link to comment
Share on other sites

Thanks Kevin (2 years later lol) that works for text, but I cannot for the life of me figure out how to get it to work for integers. I want to set N[N9] to N10, but when I use the "Variable Set Integer" command, it will not accept %N%N9%% as a variable to set. If I try to set T4 to a variable with the dereference (Variable Set String %T4% "%N%N9%%") , T4 always ends up to be "0", no matter what N9 is (and N9 <> 0!).

 

If I try to do it with the direct editor (<IVAR2:%N%N9%%:01:%N10%>), ME crashes. So does setting T5 to "<IVAR2:%N%N9%%:01:%N10%>" and using the "Run Macro in Variable %T5%" command. No, it doesn't like this at all lol ;-) It doesn't like this either (thinking that maybe I had to delimit the % symbols): <IVAR2:\%N\%N9\%\%:01:\%N10\%>

 

So I guess I'm back to a case statement based on the counter in the repeat loop unless you have any ideas on dereferencing integers (or decimals)?

 

If my post doesn't make sense, please let me know.

Basically, I want to set %N%Nx%% to %Ny%, where x is variable and y is static.

 

It's admittedly now a side-issue, but prompted by your opening phrase I ended up looking at the post that Kevin recommended up-thread . I ran the macro umvariableevaluationlevel.mex (actually called 'Sample: Variable Evaluation Level', which took some time to find after the import of 'Macro 1'!). You presumably used it successfully, albeit maybe two years ago? But when I examined the contents of the text variables (by adding a Text Display command) I found that their contents looked like this:

T10 = This variable is #10\s>

 

Is that '\s' a simple mistake, or am I missing something? Perhaps it's because I'm using ME Pro and the macro didn't convert correctly? Maybe Kevin (or the macro's author) can advise please?

 

******

 

Sleeping badly, Jim? Did you hear about the agnostic, dyslexic insomniac, who lay awake all night wondering if there really was a Dog?

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

It's all in the code. It's all in the code.

 

T10 = This variable is #10\s>

 

Is that '\s' a simple mistake, or am I missing something? Perhaps it's because I'm using ME Pro and the macro didn't convert correctly? Maybe Kevin (or the macro's author) can advise please?

Hey Terry,

 

The \s you saw is ME3's way of ending a command line in DE code. It's the "code" version of generally un-typeable box-like symbol. I believe you have Joe's Macro Express Explained. He explains it WAY better than I just did.

Link to comment
Share on other sites

Jim,

 

I'm glad you got it figured out.

 

I'll take a look at it again later, when I have a minute, but on first glance, it looks like you already know the loop will run 5 times, so you could just set N9 to 5, and your other integers accordingly (N1=1, N2=2, etc.), but I'm most likely missing something important, which is why I'll look again later.

 

Right now I have to rush out the door.

Link to comment
Share on other sites

It's all in the code. It's all in the code.

 

 

Hey Terry,

 

The \s you saw is ME3's way of ending a command line in DE code. It's the "code" version of generally un-typeable box-like symbol. I believe you have Joe's Macro Express Explained. He explains it WAY better than I just did.

 

Thanks Steve, I thought it looked familiar. But my question still stands. In real use for a macro processing grocery items, for example, I wouldn't want to see 'Pasta Sauce\s', 'Rice Pudding\s', etc.

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

I ran the macro umvariableevaluationlevel.mex (actually called 'Sample: Variable Evaluation Level', which took some time to find after the import of 'Macro 1'!). You presumably used it successfully, albeit maybe two years ago?

 

Actually, no. I just dereferenced them in my macro the way that was suggested (for the text, this was something like setting %T%T2%% to a value (if T2="1", I would be setting T1, for example)). I could not use that same method with integers or decimals due to the edit control input restrictions on the "Variable Set Integer" dialog (it only accepts N1, not N%N1% or things like that. ("Things like that" being a rather broad technical term for "things I'd rather not post lest I confuse others and myself even more").

So I defer to Steve's response about the /s character which is explained in the ME help file as well as, (I presume) the book.

 

 

Sleeping badly, Jim? Did you hear about the agnostic, dyslexic insomniac, who lay awake all night wondering if there really was a Dog?

 

I prefer to call my Higher Power "Fido", but yeah, I can relate to that. ;-)

Link to comment
Share on other sites

... on first glance, it looks like you already know the loop will run 5 times, so you could just set N9 to 5, and your other integers accordingly (N1=1, N2=2, etc.), but I'm most likely missing something important, which is why I'll look again later.

 

For 5 integers, sure, I could just do this:

 

Variable Set Integer %N1% with a Random Number

Variable Set Integer %N2% with a Random Number

Variable Set Integer %N3% with a Random Number

Variable Set Integer %N4% with a Random Number

Variable Set Integer %N5% with a Random Number

 

There are some reasons why this is not optimal coding:

 

1) 5 integers is an example, I may be setting 20 or 30

2) The integers are set in a repeat loop, not just to get the number of the integer (i.e. N1, N2, N3 etc.) to set from the counter, but to also use additional logic within the repeat loop to set each variable depending on other factors. In other words, again, this was an example for brevity. I could actually be setting N1 to something that depended on the value of N9 in another way, like maybe a random number between 1 and N9 instead of a static value for the max random number. Or I could be opening a file called "FileX.txt", where X is the value in N9. So N1 is set based on the contents of the file "File1.txt", N2 is set based on the contents of File2.txt, etc. etc. Throw in a few IF and SWITCH statements or other logic (some quite complex) and I think you can understand why I would want to do it this way.

3) Taking (1) and (2) into consideration, this would be a really messy macro, setting and modifying each variable one at a time instead of in a repeat loop where I can use the counter to "set" the variable "number" that I wish to set.

 

And now, I have to run the following macro before pressing the "Add Reply" button:

 

Variable Set Integer %N1% to 3

Variable Set String %T1% "coffee"

Variable Set String %T2% "cup"

Variable Set String %T3% "desk"

// (Place counter in N2)

Repeat with Variable using %N1%

Variable Set String %T4% "I have had %N2% %T2%"

If Variable %N2% > 1

Variable Modify String: Append "s" to %T4%

End If

Variable Modify String: Append " of %T1%, " to %T4%

Variable Modify Integer: %N3% = %N2% - 1

If Variable %N3% = 0

Variable Modify String: Append "none " to %T4%

Else

Variable Modify String: Append "%N3% " to %T4%

End If

Variable Modify String: Append "of which " to %T4%

If Variable %N3% = 1

Variable Modify String: Append "is " to %T4%

Else

Variable Modify String: Append "are " to %T4%

End If

Variable Modify String: Append "now empty on my %T3%." to %T4%

Text Type: %T4%<ENTER>

Repeat End

Variable Set String %T5% "The mind is a terrible thing to waste."

Replace " to waste" with "" in %T5%

Text Type: %T5%<ENTER>

 

:blink:

 

And Lo, it came to pass that the code was good, and there was much rejoicing:

 

"I have had 1 cup of coffee, none of which are now empty on my desk.

I have had 2 cups of coffee, 1 of which is now empty on my desk.

I have had 3 cups of coffee, 2 of which are now empty on my desk.

The mind is a terrible thing."

Link to comment
Share on other sites

Ahhhhh ok ok, I get it! Took me a while, and I had to read stuff I skimmed over before...

 

So, I finally solved it by doing this, in case anyone else out there ever tries to do the same thing:

 

Repeat Start (Repeat 5 times)

Variable Set Integer %N10% with a Random Number

Variable Set String %T4% "<IVAR2:%N9%:01:%%N10%%>"

Run Macro in Variable %T4%

Repeat End

 

Now that I actually take the time to look at your code I finally understand what it was you were trying to do. I ran into a similar problem a couple of years ago trying to get information within a Text File Process loop to get stored into incrementally higher %T% variables: Put specific info from line 1 into %T10%, info from line 2 into %T11%, etc..

 

I did run into a snag, which it looks like you've already got covered. If you start in a single-digit variable, say %T8%, when it increments to double-digits, it screws up mightily, so you want to start in the double-digits... or take the time to write code that checks for and fixes the "changeover," which I never bothered with.

 

I hated having to figure out the DE code in order to plug it into the Scripting editor. I'll try to find the thread I had going where I thought I had it all figured out and posted it, only to have Kevin come in later (I think it was Kevin) and correct it. It was a brilliant correction, too, which I appreciated. Of course, now I've forgotten it all. But let's see (maybe it's one of the threads Kevin linked to in this threat, as well)...

...

Yes, I see Kevin did link to one of my earlier posts: Here it is again

 

That's not the post I was referring to, however. I'll keep looking and either edit this one, or add a reply to it.

Link to comment
Share on other sites

Ahhhhh ok ok, I get it! Took me a while, and I had to read stuff I skimmed over before...

Now that I actually take the time to look at your code I finally understand what it was you were trying to do.

 

lol, at least SOMEONE understands! Always a good feeling. Thanks man!

 

I'm gonna install MEPro. I've been holding off because I'm not sure if my old config and macros will be safe. But I'll export them all first and, since I'm going off-topic, will stop here.

 

At least I got a solution.

 

//slight rant BEGIN

Insight told me that the checking is done by the Scripting Editor and that if I enter Direct Editor commands and run them in a variable, it could cause ME to crash, and that it's basically my responsibility. I disagreed. All they have to do is throw and catch an exception should the code in the variable not be able to be run. I understand they can't parse it to check it because the parsing and checking are done at the Scripting Editor level, but man, you can't just run the results of the conversion (to "direct editor" code) without some sort of error handling! There's 1-2 more levels below that where they can check for or catch errors (Direct Editor code -> C/C++/whatever, then C/C++/whatever calling their libraries to execute the code, or maybe just calling their library functions and passing the direct editor code... I dunno, I didn't write the thing, but they can catch the errors, and IMHO, SHOULD!) They don't have to tell people what the problem is, just that there is a problem. Not just let ME crash and burn with no error messages except the TalkBack module popping up once it notices that ME crashed...

//slight rant END

 

Hopefully they will make some fixes and catch the null pointers and such that cause ME to crash when you try to run nonsense in a variable (well, what may seem OK at the time, but turns out to be incorrect once you notice that ME crashes) ;-)

 

Thanks to all for the examples and comments! :-)

 

~jim (guy, that)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...