Friday, August 27, 2010

Working with Arrays in AS3: Part 4

In this tutorial we are going to learn how to turn a string into an Array and otherwise and also how to clone an Array.

In one of my previous tutorials I have already wrote about the split() method that turns a String into an Array. The split() method has two parameters - the delimeter (symbol(s) that the elements are separated by) and an optional parameter limit (maximal number of elements in the new Array).Here's an example:

var str:String = "Hey, check this out, man."
// Let's separate the sentence using the space symbol
var words:Array = str.split(" ");
trace(words);
// Output (5 elements): Hey,,check,this,out,,man.

// Now let's separate using the comma
var parts:Array = str.split(",");
trace(parts);
// Output (3 elements): Hey, check this out, man.

Turning an Array into a string can be done using the join() method. It is basically the opposite method to split(), it has a parameter that will be a symbol that will separate the elements in the new String. Take a look:

var myArray:Array = ["one", "two", "three"];

trace(myArray.join("|"));
// Output: one|two|three

trace(myArray.join(" "));
// Output: one two three

trace(myArray.join());
// Output: one,two,three

Now we are going to find out how to duplicate an Array.

Copying Arrays is different from copying simple types, like Numbers or Strings. The variable that contains an Array actually doesn't contain the Array data, but just a link to the memory part in your computer, that has the Array information. It is like that for optimization purposes. Simple types usually don't take a lot of memory, while Arrays can be very big. In the next example, we copy a simple variable:

var variable1:String = "Hello"
var variable2:String = variable1;

// Let's change the first variable's value and then check both variables
variable1 = "Hi"

trace(variable1); // Hi
trace(variable2); // Hello

Now let's try doing the same with Arrays:

var myArray:Array = ["one","two","three"];
var newArray:Array = myArray;

// At this point, both Arrays are the same:
trace(myArray); // one,two,three
trace(newArray); // one,two,three

// Let's change some values in the first array...
myArray[0] = "four"
myArray[1] = "five"
myArray[2] = "six"

// And now check both arrays:
trace(myArray); // four,five,six
trace(newArray); // four,five,six

As you can see, changing the first Array's values also change the second Array. Why is that? Because both of the Arrays link to the same Array data. Is this good or bad? Well, it depends on what do you need to do. However, most of the time, if you want to duplicate an Array, you'd want it to be a brand new array but with the same values.

The first way would be to use the concat() method, which creates a duplicate of the Array:

var myArray:Array = ["one","two","three"];
var newArray:Array = myArray.concat();

// At this point, both Arrays are the same:
trace(myArray); // one,two,three
trace(newArray); // one,two,three

// Let's change some values in the first array...
myArray[0] = "four"
myArray[1] = "five"
myArray[2] = "six"

// And now check both arrays:
trace(myArray); // four,five,six
trace(newArray); // one,two,three

The second way is to use the same code but replace concat() with slice(0).

var newArray:Array = myArray.slice(0);

Thanks for reading!

Related:

Working with Arrays in AS3: Part 1
Working with Arrays in AS3: Part 2
Working with Arrays in AS3: Part 3
Working with Arrays in AS3: Part 5
Working with Arrays in AS3: Part 6
Working with Arrays in AS3: Part 7

No comments:

Post a Comment