Register to post in forums, or Log in to your existing account
 

 Related 
Contents
Variables
  Local Variables
  Variable Types
  Indirect Variables
Related Links:
  Editing Functions
Variable Types [[cmud_variable_types]] 
By default, variables in CMUD are "auto-typed". This means that CMUD will automatically convert a variable between a string or numeric value as needed by your script. For example, consider the following script:
Code:
a = 123
#SHOW %concat( "Answer is: ", @a)

The %concat function takes string values and appends them together. In the above example, the number 123 is automatically converted to a string value and works just as you would expect.

This is different than many languages, such as C or C++, which are strongly typed. CMUD is weakly typed and you generally do not need to worry about the specific type of a variable.

However, in some situations, the variable type needs to be specified to obtain the correct result. One such example is when performing a Division operation. CMUD defaults to using Integer math (rather than floating-point math) because it is much faster. When performing Division, if both arguments have Integer values (no decimal places), then simple Integer division is performed, with any remainder thrown away. For example:

#SHOW (5/2)

will display "2" on the screen. The floating point result would have been 2.5, but the ".5" is thrown away when using Integer math.

To force the Division to use floating point, one of the numbers needs to be a floating point value. For example:

#SHOW (5.0/2)

will display "2.5" as expected.

Now, what if we use a Variable reference instead of the number 5 in the above examples?

Code:
a = 5
#SHOW (@a/2)


This will display "2" as in the first example, because @a has an Integer value of "5". To get the value of 2.5, you can force @a to be a floating point variable type. In the Package Editor, select Floating Point from the drop-down list of variable Types in the advanced properties panel. Now when you enter:

#SHOW (@a/2)

on the command line, it will display 2.5 because @a is forced to be a floating point variable instead of a Auto-typed variable. You can also use the %vartype function to retrieve the current type of a variable, or to set a variable to a different type within a script.

Of course, you can also use various functions, such as %float, %int and %string to convert a value to a specific type. For example:

#SHOW (%float(@a)/2)

will convert @a to a floating point value even if the "a" variable is Auto-Type.

The following variable Types are allowed in CMUD:
Auto-Type
Variable value is automatically converted between strings or numbers as needed
Integer
Value is always treated as an Integer
Expanded string
Value is always treated as a String, but variable and function references are expanded.
Literal string
Value is always treated as a literal string with no variable expansion.
String list
Value is a string list. This is a special kind of string that can contain multiple values separated by the "|" character.
Database variable
Value is a database record string. This is a special kind of string that can contain Key=Value pairs.
Array
This is a special type reserved for storing COM-based arrays. These arrays are not stored between sessions.
Floating point
Value is always treated as a floating point value (double-precision)
COM object
Variable contains a reference to a COM object. These values are not stored between sessions.
Viewer Comments [0 - Post your comments]

Jump to:  

© 2009 Zugg Software. Hosted by Wolfpaw.net