how to auto variables

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
paranova9
Newbie
Posts: 17
Joined: Wed Jul 29, 2009 9:19 pm

how to auto variables

Post by paranova9 » Wed Jul 29, 2009 9:35 pm

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

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Wed Jul 29, 2009 10:14 pm

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!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu Jul 30, 2009 12:41 am

Or mathematically:

Code: Select all

Let>k=1
Let>a=1
Let>q={(%k%*10)+%a%}
MDL>q
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.

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Creating variables

Post by gdyvig » Thu Jul 30, 2009 3:14 am

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:

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"
One difficulty is that the % symbols cannot be nested.
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" 
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:

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

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jul 30, 2009 6:30 pm

[email protected] wrote:where let>k+ a=1 will produce a variable called 11
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.

- 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
To state it simply:
- 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
Hope this is useful.

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu Jul 30, 2009 8:01 pm

JRL wrote:
[email protected] wrote:where let>k+ a=1 will produce a variable called 11
I could be wrong but I interpret this the same way Gale did.
In retrospect I think you are both correct, naming a variable "11" just didn't smell right to me at the time.

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

A few more examples

Post by gdyvig » Thu Jul 30, 2009 8:08 pm

JRL' explanation is less confusing.

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

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

paranova9
Newbie
Posts: 17
Joined: Wed Jul 29, 2009 9:19 pm

Post by paranova9 » Sat Dec 05, 2009 9:34 am

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

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts