Learning MEL – Step #3 – Using Variables – Part I

In this part I am going to introduce one of the fundamental parts in Programming. Variables. Variables are little bags that store in themselves something useful. This is the most important feature of variables. They store data. For instance, you “declare” the variable X to be assigned 2, and later in the script, you write 5 + X (Just remember old days of school’s math formula), it equals 7. You can ask this question that why then not just writing 2 instead of X, what’s the use of it? There are several points important about variables, within which, you can find your answer also.

Variables are dynamic storage. For instance a value might just be unknown or changing in time and you want an area in script to grasp the change. Let’s give an example: There is a form, an input for the user to enter a number, then how do you know what value is entered? You retrieve the value from the input form and place it in a variable. By doing this, you assign the value to a variable. Look, = never means equal in Programming but is called assignment, and equal sign is ==. Later we would explore it more.

Variables ease the process of programming by referencing all values to one storage. For instance you want to say that how much a person does drink water during a year. You calculate the amount of water within a day and then multiply it by 365. And this calculation happens several times during the script.For the purpose of a neat script, a maintainable script and a safe script you must assign 365 to a variable. the variable let’s say is year. so whenever you use “year”, you simple is referencing it to storage which has 365 in itself. To put it in a nutshell, variables are words, exactly words which are representing a specific number. And there is something special about them, you can only declare a unique variable. Its name must not be mixed with another variable. If you use a name two times, you simply say to Maya to ignore the first declaration from the second-declaration on.

Another crucial limit of variable is that you cannot begin their name with numbers, if so, then Maya reports syntactical error.

After this introductory part I go in dealing with variables. For declaring a variable in Maya you need to put a “$” (without quotations) prior to the name.A Dollar Sign. Therefore:

cubeLength (this is not true)

$ cubeLength (this is not true; note the space after the dollar sign).

Anyway. The following lines give you the way it works. Here I want to print  “Hello World!” using variables.  Doing so, we see it in practice;

string $text = “Hello World!”;

print ($text);

If we dissect the first line, we would have the following parts:1) string

2) $text

3) “Hell World”

4) ;

Let’s explain them. What is string? string is any character in a programming language. It is a “data type”. Just you use it whenever dealing with text. Instead of string, you can say “text” even. Or in other words, it is “traditionally a sequence of characters”.

Examples of string:

“Table” “tree” “That Man Standing There” “Operating System” “ME Software” and …

Pay a close attention to quotation marks around each string. quotation marks are essential, without them, the text are not known as to be string. For instance:

(2 + 2) is a mathematical equation and if you give it you print function, Maya would output it as:

4

yes Maya would output 4 since it computes them and then prints 4. While if you give “2 + 2” to print function Maya would output:

2 + 2

Yes, because Maya sees it as a set of characters and not anything more.

Now move to the second part, $text? I have already explained the variable naming and I think you can now name variable easily.

Here you reach the “=” sign after the variable name. This is not equal sign, but it is assignment, by using this sign you are assigning a value to a variable. You could have already finished the declaration of the variables using ; and not = . The in following lines you would assign them value. Something like this:

string $text;

$text = “Hello World!”;

Note that only and only the first declaration of variable is accompanied by its Data Type.

For numbers you have to use “int” (without quotation) data type. Which is integer, but if your number has decimal, then you can use “int: and must use “float”(without quotation). However, if you accidentally give a number with decimal to an integer variable, decimals are stripped. and But you can use integer numbers with float data type. That’s why I most of the time use float data type when I’m not sure what happens to number in the future.

Examples of float:

2.1314556      10.5       5.89    3.14    1.8952434134   -5.0  -3   6 10     112   1100.1

Examples of integer:

5 8     12  112   6   -10    -78     0   23

Now I guess you have a basic understanding of what is variable and a primary knowledge about their usage. Lets do some calculations:

int $number_one = 10;int $number_two =  5;

int $result = $number_one  + $number_two;

print ($result);

This is the end of the first part of Variables. I hope it’s been useful for readers, and by keeping in touch, you can get more tutorials in this series.

Mostafa Talebi

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s