ActionScript: How to assign arrays by VALUE rather than REFERENCE? - actionscript-3

I find it unfamiliar to work with ActionScript's array assignment by reference methodology. I understand what it's doing, but I somehow find it confusing to manage many arrays with this methodology. Is there a simple way to work with ActionScript arrays where the array assignment is by VALUE rather than REFERENCE? For example, if I want to assign oneArray to twoArray without linking the two arrays to each other forever in the future, how to do it? Would this work?
var oneArray:Array = new Array("a", "b", "c");
var twoArray:Array(3);
for (ii=0; ii<3; ii++) { twoArray[ii] = oneArray[ii]; }
The intent is to be able to change twoArray without seeing a change in oneArray.
Any advice how to assign arrays by VALUE instead of REFERENCE?
---- for reference ----
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html
Array assignment is by reference rather than by value. When you assign one array variable to another array variable, both refer to the same array:
var oneArray:Array = new Array("a", "b", "c");
var twoArray:Array = oneArray; // Both array variables refer to the same array.
twoArray[0] = "z";
trace(oneArray); // Output: z,b,c.

Looks like you are looking for slice method. It returns a new array that consists of a range of elements from the original array.
var oneArray:Array = new Array("a", "b", "c");
var twoArray:Array = oneArray.slice();
twoArray[0] = "z";
trace(oneArray);
EDIT: Note that slice does a shallow copy, not a deep copy. If you are looking for a deep copy then please follow the link specified in the comment.

You can clone the array to guarantee two seperate copies with the same values in each Array element:
var oneArray:Array = new Array("a", "b", "c");
var twoArray:Array = oneArray.concat();
twoArray[0] = "z";
trace(oneArray); // Output: a,b,c
Hope this is what you're looking for.

If I understand the question correctly, you could do this:
var collection= new ArrayCollection(["a", "b", "c"]);
var clonedCollection = new ArrayCollection(ObjectUtil.copy(collection.source) as Array);
// a, b, c
trace(collection.toString());
// a, b, c
trace(clonedCollection .toString());
clonedCollection.removeItemAt(0);
// a, b, c
trace(collection.toString());
// b, c
trace(clonedCollection .toString());

You can create a clone function to copy the object using the ByteArray.writeObject and then out to a new object using ByteArray.readObject, as described in livedocs.adobe.com - Cloning arrays.
Note that writeObject and readObject will not understand objects more complex than Object and Array.

Related

AS3 - getting variable named in order within a function

I am sorry if this is a beginner's question.
I made some Arrays named like map01, map02 and so on... As you can see, I'm making a tile-based flash here. And I need to make a function that when you input a number like: createmap(1); it will get the variable map01 and use the information.
Can I do anything like: var temp:Array = Array(["map" + valueInput]);??
Please tell me if you need anything more.
First, instead of having variables with indices in their names, you should create an array of them. Here, an array of arrays.
So you just have to call var temp:Array = maps[valueInput] as Array;.
If you really don't want to do that and stick with your n variables, you can write
var index:String = valueInput.toString();
if (index.length == 1)
index = "0" + index; //have the index on two digits "01", "02"
var temp:Array = this["map" + index];
Note that it will only work for your 99 first variables (oh God...)

actionscript remove (concat?) sub-arrays

I have multiple sub-arrays in one huge array - MasterArray- meaning that the sub-arrays are already INSIDE the MasterArray. I would like to "fuse" all of those sub-arrays - to remove those [ ] square brackets.
I would like to avoid the "concat" method because the arrays are already inside the MasterArray. Is there a command/method how to do this?
Thank you.
var englandCities:Array = [London, Manchester, Leeds];
var franceCities:Array = [Paris, Orleans, Avignon];
var europeanCities:Array = [englandCities, franceCities];
I would like to point let's say...to "London" nested in the europeanCities array somehow.
After I try to trace it, it gives me "englandCities", which makes sense.
trace(europeanCities[0]);
// displays "englandCities"
// how can I make it display "London" ?
How can I make the europeanCities array to display "London" ?
I NEED TO REMOVE THOSE SQUARE BRACES from the "europeanCities" array WITHOUT using the concat() thingie...
OKAY let me rephrase this a bit. My master array:
var europeanCities:Array = [englandCities, franceCities];
equals to
[[London, Manchester, Leeds], [Paris, Orleans, Avignon]];
am I right? And now, how to remove the inner brackets in order to get something like this:
[London, Manchester, Leeds, Paris, Orleans, Avignon];
And please, keep in mind, that the array is much longer than englandCities and frenchCities....there are like...30 different Cities.
You can concat those together easily, and it really is the simplest option:
var englandCities:Array = ["London", "Manchester", "Leeds"];
var frenchCities:Array = ["Paris", "Orleans", "Avignon"];
var masterArray:Array = [englandCities, frenchCities];
var europeanCities:Array = new Array();
for each(var country:Array in masterArray) {
europeanCities = europeanCities.concat(country);
}
trace(europeanCities); // London,Manchester,Leeds,Paris,Orleans,Avignon
I'm not sure I understand your reason for avoiding concat for this, unless the issue is it that you don't want to duplicate the values. (So modifying englandCities[0] will also modify europeanCities[0].)
If your cities are Objects rather than primitive Strings, a concatenated Array will work fine. If they are primitives though, there's no way to do this with an Array. You could however write a function to provide similar behaviour like this:
var englandCities:Array = ["London", "Manchester", "Leeds"];
var frenchCities:Array = ["Paris", "Orleans", "Avignon"];
var allCities:Array = [englandCities, frenchCities];
function europeanCities(id:int):String {
var cityID:uint = 0;
while (id > allCities[cityID].length - 1) {
id -= allCities[cityID].length;
cityID++;
}
return allCities[cityID][id];
}
trace (europeanCities(0)); // London
trace (europeanCities(5)); // Avignon
Create an empty array, then traverse the masterArray taking any sub-arrays, and do a concat() for your new array. This will make you another array that's flat, without disturbing master array.
I just write this here because it is possible.
If you insist on not using concat here is one bad solution:
// join elements into a comma delimited string
var s: String = europeanCities.join(',');
// Split the string with delimiter as commas
europeanCities = s.split(',');
Since the subarray elements automatically will be joined with ',' regardless of join delimiter and our join delimiter is already ',' this will work.
But this solution is cpu intensive and not optimal.

Splice then re-index array in ActionScript 3

I want to remove the first four indexes from the array using splice(), then rebuild the array starting at index 0. How do I do this?
Array.index[0] = 'one';
Array.index[1] = 'two';
Array.index[2] = 'three';
Array.index[3] = 'four';
Array.index[4] = 'five';
Array.index[5] = 'six';
Array.index[6] = 'seven';
Array.index[7] = 'eight';
Array.splice(0, 4);
Array.index[0] = 'five';
Array.index[1] = 'six';
Array.index[2] = 'seven';
Array.index[3] = 'eight';
I am accessing the array via a timer, on each iteration I want to remove the first four indexes of the array. I assumed splice() would remove the indexes then rebuild the array starting at 0 index. it doesn't, so instead what I have done is created a 'deleteIndex' variable, on each iteration a +4 is added to deleteIndex.
var deleteIndex:int = 4;
function updateTimer(event:TimerEvent):void
{
Array.splice(0,deleteIndex);
deleteIndex = deleteIndex + 4;
}
What type of object is "Array" in the code you have shown? The Flash Array object does not have a property named "index". The Array class is dynamic, which means that it let's you add random properties to it at run time (which seems to be what you are doing).
In any case, if you are using the standard Flash Array class, it's splice() method updates the array indexes automatically. Here is a code example that proves it:
var a:Array = [1,2,3,4,5];
trace("third element: ", a[2]); // output: 3
a.splice(2,1); // delete 3rd element
trace(a); // output: 1,2,4,5
trace(a.length); // ouput: 4
trace("third element: ", a[2]); // output: 4
If I am understanding what you want correctly, you need to use the unshift method of Array.
example :
var someArray:Array = new Array(0,1,2,3,4,5,6,7,8);
someArray.splice(0,4);
somearray.unshift(5,6,7,8);
Also, you are using the Array Class improperly, you need to create an instance of an array to work with first.
The question is confusing because you used Array class name instead of an instance of an array. But as the commenter on this post said, if you splice elements, it automatically re-indexes.
im not sure what you want to do, but Array=Array.splice(0,4) should fix somethin..

How to declare a filled Vector?

I am using Vectors in Flash 10 for the first time, and I want to create it in the same way I used to do with Arrays, e.g:
var urlList : Array = [url1, url2, url3];
I have tried various different methods but none seem to work, and I have settled on the following as a solution:
var urlList : Vector.<String> = new Vector.<String>();
urlList.push(url1, url2, url3);
Is this even possible?
When it doubt, check the AS3 docs. :)
var urlList : Vector.<String> = new <String>["str1", "str2", "str3"];
trace(urlList);
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Vector.html#Vector()
Direct quote of the line I adapted this from in the documentation:
To create a pre-populated Vector instance, use the following syntax instead of using the parameters specified below:
// var v:Vector.<T> = new <T>[E0, ..., En-1 ,];
// For example:
var v:Vector.<int> = new <int>[0,1,2,];
You coerce an array to a Vector:
var urlList:Vector.<String> = Vector.<String>([url1, url2, url3]);

AS3 How to make a kind of array that index things based on a object? but not being strict like dictionary

How to make a kind of array that index things based on a object? but not being strict like dictionary.
What I mean:
var a:Object = {a:3};
var b:Object = {a:3};
var dict:Dictionary = new Dictionary();
dict[a] = 'value for a';
// now I want to get the value for the last assignment
var value = dict[b];
// value doesn't exits :s
How to make something like that. TO not be to heavy as a lot of data will be flowing there.
I have an idea to use the toString() method but I would have to make custom classes.. I would like something fast..
Why not make a special class that encapsulates an array, put methods in there to add and remove elements from the array, and then you could make a special method (maybe getValueByObject(), whatever makes sense). Then you could do:
var mySpecialArrayClass:MySpecialArrayClass = MySpecialArrayClass();
var a:Object = {a:3};
var b:Object = {a:3};
mySpecialArrayClass.addElement(a,'value for a');
var value = mySpecialArrayClass.getValueByObject(a);
I could probably cook up a simple example of such a class if you don't follow.
Update:
Would something like this help?
http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/
Update:
Could you use the === functionality? if you say
if ( object === object )
it compares the underlying memory address to see if two objects are the same reference...