Lists
Tutorial about how to use lists in MIFL.
Lists
A "list" is a data type that stores zero or more values in
sequence. A list can store any mix of values.
Lists are similar to ActiveScript's arrays.
C/C++ don't even contain lists.
To create a list in code, use the left bracket ([), followed
by some comma separated values, and finished up with the
right bracket (]). A list can contain other lists.
Some examples of lists are:
-
[1, 54, 43, 23] - A list containing 4 numbers.
-
["Mike", "George", "Amy", "Chris"] - A list containing 4 names.
-
[1, "hello", 43, true] - This list has a mix of data types,
including numbers, strings, and booleans.
-
[1, ["hello", 43], true] - This list contains another
list as it's 2nd element.
-
[] - An empty list, with no elements.
List operators
Many of the operators work with lists:
-
x = [53,324] + 435 - This fills x
with [53, 324, 435], appending data onto the
list.
-
x = [53,324] + [435, 89] - This fills x
with [53, 324, 435, 89], appending the elements
from the second list onto the first. If you wanted to
generate [53, 324, [435, 89]] you'd need to call
[53, 324].ListConcat ([435, 89]).
-
x += [54,34] - This is interpreted as "x = x + [54,34]".
-
[1,2] < [2,2] - The <, <=,
> >=, ==, and === operators do element-by-element comparisons
of the two lists. Any string comparisons are case sensative.
You need to use ListCompare() to do a case
insensative compare.
-
x = [54,34]; y = x[1]; - Fills y in with the 2rd element (0 being the
first element). y = 34.
-
x = [54,34]; x[1] = "hello"; - This fills x with [54,34],
but then changes the 2rd element to "hello", resulting
in [54, "hello"]. If the index is beyond the end of the list, the
list will be extended with undefined values.
List "methods"
Lists support "methods", which are functions that automatically
modify the list that they appear after. Example: To use the
method "ListAppend" with the list in variable x,
just use "x.ListAppend()".
Below is a partial list of methods supported by lists.
A complete list can be found under the "Reference" section of
help, in "Data types", "Lists".
-
ListAppend - Append a list or other data type onto the end of the list.
-
ListCompare - Case insensative comparison between lists.
-
ListInsert - Insert one list into another.
-
ListNumber - Returns the number of elements in the list.
-
ListSort - Sorts a list alphabetically or numerically.
-
ListSlice - Cut a portion of the list out and into a new list.
List reference counting
When you create a list, in the background MIFL allocates some
memory for the list. Associated with the memory is a "reference count"
that lets MIFL know if the list is still remembered by
any variables, parameters, properties, etc. If the MIFL
script "forgets" about the list then the memory is automatically
deleted. This ensures that you won't get memory leaks from lists
that are no longer in use.
It also causes some noteworthy behavior. Look at the following
code:
var x = [1,4,6];
var y = x;
x.ListAppend (99);
return y;
|
This function will return a list, [1,4,6,99].
Here's why:
-
"var x = [1,4,6]" creates a list and then stores
a reference to it in x.
-
"var y = x" does not copy the list, but copies
the reference to the list. Therefore, x and y refer
to the same list.
-
"x.ListAppend(99)" appends the element, 99, onto the
list that both x and y are referencing.
-
"return y" just returns another reference.
This can get you if you're not careful because you may assume
that "y = x" will copy the list, but it only copies the
reference. Then, if you modify x you'll find that y is also
modified, or vice versa.
(Conversely, this can be a very useful feature.)
If you want y to contain a copy of the list (so that modifying
it won't affect x) then use:
This duplicates the list referred to by variable x,
and sets y to the refer to the new list. It also
duplicates all the data within the list, so any sublists or
strings will also be cloned.
Note: Concatenation with the + operator creates a
copy of the list, which is different behavior than ListAppend(),
which modifies a list in place.
Share with your friends: |