Learning MEL – Step #8 – Arrays (Part 2)

We already have covered arrays basic points. In this part I go over working with arrays.

When a new thing is introduced, in any place and field, from math to technology, in religion or politics, in CGI or in traditional painting, it comes with associated issues/advantages/components/tools etc. Therefore array also has it own functions and methods. Do not forgot that array is a hero in Computer Programming.

Related Functions:

Functions to be solely used with arrays are:

sort(), size() and clear();

Above three functions are general functions related to arrays. There are many functions used with array that are both complex and far from our point of study, thus I leave them uncovered. Let’s begin explaining our triple functions.

sort(): Use this function simply by placing the array inside the () of the function.

sort($array);

This function would sort the element in an array in “ascending order” and returns a sorted array. However, be aware that the array does not get sorted internally, only returned values from sort() function would sort the output. I return to this point later. Just for now, focus on this example:

string $examples[] = {“hello”, “laugh”, “that man”, “somewhere”, “awful”};
print $examples;

Regularly it outputs based on the order of declaration:

print $examples;
hello
laugh
that man
somewhere
awful

But if you use sort() function, the result is this:

sort($examples);

// Result: awful hello laugh somewhere that man //

However you if you like to keep the result of examples, you have two choices:

1 – Put the result in a new array, like this:

$examples = sort($examples);

Look at the above example. There is not need to use array curly brackets after $example, because MEL already knows it is an array, and you are not going to reintroduce it to MEL, but just what you indeed do is assigning a new set of data.

2- Declare a new array. look at below:

string $sorted_array[] = sort($example);

Look, do not mess up the above $example array with method number 1. The above $example variable is the array unsorted array.

Both of above methods work. Sometime you need only  and only direct printing without keeping the result necessarily in an array. In that specific case, use sort() directly.

size(): Use this function simply by placing the array inside the () of the function.

size($array);

size() is a very very very important function which returns the number of elements in an array. why is it important? Because it dynamically allows you to know about the size of an array whenever you want to. For instance, if you want to print the list of selected nodes, how you would do that? of course you don’t know, since I have not yet covered it, but I will cover it perfectly in next tutorial. title Loops.

Look, this function would return an int number.

clear(): Use this function simply by placing the array inside the () of the function.

This function as its name is explanatory, would clear the array. It does not, however, undeclare it, it just remove every elements in the array , and let you have an empty array without any element.

Therefore, if I do clear our $example array, it will output:

clear ($example);

print ($example);

// Result: //

I wish you learn whatever we do cover here, as these are free tutorials.

Learning MEL – Step #7 – Arrays (Part 1)

In this part we are going to cover arrays in MEL. Here I do explain what is array. I also would tell you three functions used exclusively with arrays.

Arrays are only and only a set of variables combined in one variable. Let’s give an example:

You have in a regular variable declaration:

string $text = “example”;

string $text_2 = “example_25”;

If you want to print them you simple would refer to them directly; such as:

print ($text);

or:

print($text_2);

The above example is not new to you, since we have already covered them. But what if you wish to put both variables in one variable? The solution is simple, array. But then how you can access them? Simply by using index of the element in the array. Now, I go into a comprehensive discussion of arrays in MEL.

How to declare?

The process is the same but with some extra steps to take:

after the name of variable, without any space, “[]” sign must be used.

If you want to assign it some sorts of data, then you must open a “{” and then write your element, separate them only and only with “,” and then close it with “}”. In the following parts it would be more explored. Look at this example:

string $array[] = {“element1”, “element2”, “element3”};

Why must array be used?

Never think that arrays are only an extra option to ease your path. They are as essential as simple variables are, and as inevitable as the use of simple variables are. Even sometimes arrays are the only option to use due to the language process at a certain scope. For instance, there are functions in MEL language that return the data  in an array, therefore to store their result one has to use an array, and nothing else. I will cover this part more in the future lessons, because  now it would be confusing.

When to use arrays?

Arrays are used largely in two ways:

Declaration:

First, when you are going to collect a bunch of data within separate sheets. Then one might ask what’s wrong with simple variables? You can not manage them properly, and also accessing them is nothing but a headache. Furthermore, if you want to use only regular variables, the look of your scripts would be ugly enough to prevent other programmers to apply modifications to your script. Any way, MEL is awkward in arrays. I do web programming with PHP, and it supports the use of more complex variables and thus easier workflow.

Stored Result:

This section makes the use of variables necessary. MEL has a function such as “ls” which only returns array. The below statement returns the list of selection.

ls -sl;

Most of the time, almost 99.99999 percent of situations there is a need to store the selection list. Because you are going to use the retrieved data;  Maya reports an error telling you that can not store data of array type into a regular variable. Even if there only be one object selected, which means an element that could be stored in a simple variable, still Maya does not accept it and errs; we now draw this conclusion that in some situations arrays are obligatory. The above statement could be written as so:

string $selection[] = ls -sl;

Now if I do print the $selection, it returns the full list of selected objects; however if there is only one object, you can refer to it like this:

$selection[0];

Index of arrays begin from 0, and not 1.

Have you ever seen (surely you have) a tool in Maya works with two selected object; you must select an object and shift+select the second object? These are the examples:

Constrainting the objects: $selection[0] works as the parent (constraintor).

Curve to Surface: $selection[0] works as the profile or whatever curve.

and many other examples.

However you can directly declare an array with several elements:

string $array_variable = {“Hello” , “Goodbye”, “Function”, “Table”, “home”};

Now we have a variable with 5 subvariables. However, this is wrong to say subvariables, items in array are called elements. We have an array with five elements, it means the last index in array is 4 (remember we have already told index of array begins from zero). Well, to talk more about it: this is an array with string data, however you can use mutiple-data array. Look at the following example:

string $array_variable = {“Hello” , “Goodbye”, “Function”, “Table”, “home”, 2, -0.53574818747, true};

Now this is an array with all sorts of data in; string, int, float and Boolean.

Conclusion:

Arrays are variables with capability of storing several variables with various data-type. Accessing them is possible only by using the relative key(index). Never forgot to use “[]” after the variable declaration so that MEL understand what to declare. In the next tutorial we would apply array-specific functions.