how to auto variables
Moderators: JRL, Dorian (MJT support)
how to auto variables
I'm trying to do something like this
Let>k=1
Let>a=1
let>k+ a=1
where let>k+ a=1 will produce a variable called 11
Let>k=1
Let>a=1
let>k+ a=1
where let>k+ a=1 will produce a variable called 11
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
Is this what you want?
Code: Select all
Let>k=1
Let>a=1
Let>vNew=%k%%a%
MessageModal>New variable is %vNew%
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
-
- Automation Wizard
- Posts: 1101
- Joined: Fri Jan 07, 2005 5:55 pm
- Location: Somewhere else on the planet
Or mathematically:
Note that either solution will work, Bob's vNew can be used as a numeric variable in a calculation too, even though it is generated with a string function.
Code: Select all
Let>k=1
Let>a=1
Let>q={(%k%*10)+%a%}
MDL>q
Creating variables
Hi paranova,
Bob and Me_Again's code will address the variable whose name is 11 if it already exists but will not create it.
A script can dynamically produce new variables provided the system variable VAREXPLICIT is set to 0. That is the default value so normally you do not need to worry about it.
When using the Let command to create variables, the other requirement is that the dynamic variable name be to the left of the ">".
So you need something like this:
One difficulty is that the % symbols cannot be nested.
This will not work:
This will work:
Another difficulty is now that 11 is a variable name the arithmetic commands may work wierd when VAREXPLICIT is set to 0. I believe any complex expressions (anything inside of braces{} ) is always VAREXPLICIT 1. For this reason most people would include letters in the name of their variables or set the up in an array, like this:
Confused?
I'm was confused so I had to test this code out to make sure it works as described.
Not really what you were looking for?
Still might be fun to play with.
Gale
Bob and Me_Again's code will address the variable whose name is 11 if it already exists but will not create it.
A script can dynamically produce new variables provided the system variable VAREXPLICIT is set to 0. That is the default value so normally you do not need to worry about it.
When using the Let command to create variables, the other requirement is that the dynamic variable name be to the left of the ">".
So you need something like this:
Code: Select all
Let>VAREXPLICIT=0
Let>k=1
Let>a=1
Let>vNew=%k%%a%
Let>%vNew%=1
Let>VAREXPLICIT=1
MessageModal>New variable name is %vNew%
//Above displays "New variable name is 11"
MessageModal>The variable 11 has a value of %11%
//Above displays "The variable 11 has a value of 1"
This will not work:
Code: Select all
MessageModal>The variable %%vNew%% has a value of 1.
This will work:
Code: Select all
Let>vVarValue=%11%
MessageModal>The variable %vNew% has a value of %vVarValue% or %11% or 1.
//Above displays "The variable 11 has a value of 1 or 1 or 1"
Code: Select all
Let>VAREXPLICIT=0
Let>k=1
Let>a=1
Let>vNew=vNumVar_%k%%a%
Let>%vNew%=1
//with different valuds of k and a you could create an array of variable names:
//vNumVar_11, vNumVar_12, vNumBar_21, etc
//this array of variable names will not interfere with normal arithmetic operations.
Confused?
I'm was confused so I had to test this code out to make sure it works as described.
Not really what you were looking for?
Still might be fun to play with.
Gale
I could be wrong but I interpret this the same way Gale did. You want to create a variable whose value is a variable. I think everything Gale said is correct. And he made two important points.[email protected] wrote:where let>k+ a=1 will produce a variable called 11
- The system variable Var_Explicit must be set to its default of "0".
- if you use this technique, be very cautious, especially if you are assigning new values to numbers.
I was just hoping to simplify a little and then elaborate a little.
This works.
Code: Select all
Let>k=1
Let>a=1
Let>%a%%k%=1
MDL>11
//message above will display 1
- If you use the let function to assign a value to a variable
- And if the variable has percents around it
- And if the variable already has a value
- You will be assigning the new value to the value of the variable rather than to the variable.
I have a more lengthy explanation here.
One use I've found for this technique, is when I want to create a result variable for a subroutine. For example, the following assigns the end value of the subroutine to the variable named "result"
Code: Select all
Gosub>BindNames,John,Smith,result
MDL>result
SRT>BindNames
Let>%BindNames_var_3%=%BindNames_var_1% %BindNames_var_2%
END>BindNames
-
- Automation Wizard
- Posts: 1101
- Joined: Fri Jan 07, 2005 5:55 pm
- Location: Somewhere else on the planet
In retrospect I think you are both correct, naming a variable "11" just didn't smell right to me at the time.JRL wrote:I could be wrong but I interpret this the same way Gale did.[email protected] wrote:where let>k+ a=1 will produce a variable called 11
A few more examples
JRL' explanation is less confusing.
Here are a few more examples showing how VAREXPLICIT works and a potential problem involving embedded or trailing spaces.
Leading, embedded, and trailing spaces can be parts of variable names - or can prevent a string of text from being interpretted as a variable.
Gale
Here are a few more examples showing how VAREXPLICIT works and a potential problem involving embedded or trailing spaces.
Code: Select all
Let>k=1
Let>a=1
Let>%a%%k%=1
MDL>11
//message above will display 1
//Next line has a trailing space after the 11
MDL>11
//message above will display 11
//Next line has an embedded space between 11 and :
MDL>11 :%11%
//message above will display 11 :1
Let>VAREXPLICIT=1
//Next line has no trailing spaces
MDL>11
//message above will display 11
MDL>11 :%11%
//message above will display 11 :1
Let>VAREXPLICIT=0
Let> 12=2
MDL>12
//message above will display 12
MDL> 12
//message above will display 2
Gale
thank you everyone, I have found this works with no need to mess with varexplicit
Code: Select all
Let>l=1
Let>n=3
let>lv2=a%l%v%n%
let>a%l%v%n%=9
Mdl>%lv2% %a1v3%
// displays a1v3 9