AS3: Merge multiple 2d Arrays - indexOf not working - actionscript-3

I want to merge multiple 2dim Arrays which are of the type [unique number][a fix number]
Since I would like to merge them (and have unique results) I'm searching inside the "retArr" if array[unique number] is present.
Array1: retArr
Array2: arg (multiple 2dim Arrays)
// before here is an additional "for each"-loop which gives me in every iteration a "new" arg-Array.
for (var p:uint = 0; p<arg.length; p++){
if(retArr.length ==0){
var tmp:Array = new Array();
tmp.push(arg[p][0]);
tmp.push(arg[p][1]);
retArr.push(tmp);
}
else{
for(var i:uint = 0; i<retArr.length; i++){
if (retArr[i].indexOf(arg[p][0]) == -1){
var tmp:Array = new Array();
tmp.push(arg[p][0]);
tmp.push(arg[p][1]);
retArr.push(tmp);
break;
}
}
}
}
I think the line
if (retArr[i].indexOf(arg[p][0]) == -1)
is my Problem, since I'm getting double-results in my retArr.
Can anyone help me out please?

To summarize: You have an array of arrays and each array in the parent array has two elements. You want to filter out all the arrays that have duplicated values at index 0. Is this correct?
If so the problem is in your second for loop. Say this is what you have in arg:
var arg:Array = [[0, 1], [2, 3], [2, 3]]
On your first iteration, retArr contains nothing, so [0, 1] is added and the second for loop doesn't run
On your second iteration, the second for loop executes once because the first element of [2, 3] does not appear in [0, 1]
Now here's the problem: On your third iteration, the second for loop executes. It checks to see if [0, 1] contains 2. It doesn't, so it adds it to retArr. It never even checks to see if [2, 3] contains 2.
What you need to do is loop through all elements of retArr, and if there were no matches at all, then you can safely add the array.
var matchFound:Boolean = false;
for(var i:uint = 0; i<retArr.length; i++){
if (retArr[i].indexOf(arg[p][0]) != -1){
matchFound = true;
break;
}
}
if( ! matchFound) {
var tmp:Array = new Array();
tmp.push(arg[p][0]);
tmp.push(arg[p][1]);
retArr.push(tmp);
}
There is another problem: You are filtering out an array even if it's element 0 matches another array's element 1. To only filter out array's that have duplicate element 0's, change this
if (retArr[i].indexOf(arg[p][0]) != -1){
to this
if (retArr[i].indexOf(arg[p][0]) == 0){
It would probably be faster and less complicated if you just did this:
if(retArr[i][0] == arg[p][0]){

It looks like this should be possible by using splice or concat
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#concat%28%29
you can just attach arrays to retArr, without calculating the length and iterating

Related

How to add numbers in array via a loop in AS3?

var arr: Array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
I can add these numbers to an array separately like this but how do I add 1 to 50 at once instead of typing it all the way through?
for (var i:Number=1; i<=50;i++){
var arr:Array(i) = [i];
}
function randomize(a: * , b: * ): int {
return (Math.random() > .5) ? 1 : -1;
}
trace(arr.sort(randomize));
I am trying to implement something like this.
Thank you.
Pretty simple. You can address the Array's elements via square bracket notation. Works both ways:
// Assign 1 to 10-th element of Array A.
A[10] = 1;
// Output the 10-th element of A.
trace(A[10]);
Furthermore, you don't even need to allocate elements in advance, Flash Player will automatically adjust the length of the Array:
// Declare the Array variable.
var A:Array;
// Initialize the Array. You cannot work with Array before you initialize it.
A = new Array;
// Assign some random elements.
A[0] = 1;
A[3] = 2;
// This will create the following A = [1, null, null, 2]
So, your script is about right:
// Initialize the Array.
var arr:Array = new Array;
// Iterate from 1 to 50.
for (var i:int = 1; i <= 50; i++)
{
// Assign i as a value to the i-th element.
arr[i] = i;
}
Just keep in mind that Arrays are 0-based, so if you forget about index 0 it will remain unset (it will be null).

Sorting a multidimentional array and moving all the same values to the last array in as3

I'm currently working on a primitive graphic for the inventory
that shows how much space is currently taken and how much theres
left.
It does this by showing 5 rows of 20 squares
(Gray = free space, yellow =space taken).
Now, my problem is that I want to find all values that
are 1 and put them in the last array.
So that the array goes from:
var myMap:Array = [
[1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0],
[1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0],
[1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0],
[1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0],
[1,0,0,0,0,0,1,1,0,1,1,0,0,1,1,0,0,0,0,0]
];
to
var myMap:Array = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
];
Basically I want to sort the entire array so that all zero's come first
and all 1's get put in the last one till its full, then the second last one and so on.
I'm away I will most likely need an algorithm for this but I am hoping theres an easier way to do this. I'm usually a C# developer so AS3 is not my strongest suit.
hopefully someone understood what I ment and can help me, its 4:30 am, so I might not be as lucid as I'd like.
~Thanks
EDIT
Added the code so people can get more of a informed look:
Keep in mind most of the names and so on are placeholders, currently I just need it working.
private function _showInventoryGraphic()
{
var mapWidth = 20;
var mapHeight = 5;
var myMap:Array = [
[1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0],
[1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0],
[1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0],
[1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0],
[1,0,0,0,0,0,1,1,0,1,1,0,0,1,1,0,0,0,0,0]
];
var posX:int = 15;
var posY:int = 15;
var startPosY:int = 250;
for (var i:int=0; i<mapHeight; i++)
{
for (var u:int = 0; u < mapWidth; u++)
{
if (myMap[i][u] == 0)
{
var grayRect:Shape = new Shape;
grayRect.graphics.beginFill(0x808080);
grayRect.graphics.drawRect(posX*u, startPosY, 10,10);
grayRect.graphics.endFill();
addChild(grayRect);
}
if (myMap[i][u] == 1)
{
var yellowRect:Shape = new Shape;
yellowRect.graphics.beginFill(0xFFFF00);
yellowRect.graphics.drawRect(posX*u, startPosY, 10,10);
yellowRect.graphics.endFill();
addChild(yellowRect);
}
}
startPosY += posY;
}
}
After trying it out from a few different approaches, I think the biggest challenge is the fact that it is a 2d array.
The first attempt was similar, but like quicksort, where there were 2 pointers at either end of the 2d array and they moved 'inwards' looking for a scenario where the first pointer was a '1' and the last pointer was a '0' and swapping the two, until the pointers met in the 'middle'. I gave up on this method while trying to properly decrement and increment the linear counters in a 2d array. =b
The second attempt was by keeping track of the last index in the array that was as empty and the last index in the array that was full. Then within the addItem and removeItem methods I'd put the item in the correct spot and update the counters to avoid a sort. This seemed to work, but maybe a bit messy and perhaps harder to maintain in the future. It sorts the result in separate arrays.
In the third attempt for a 'direct sort', I think the most simple way then is just flatten the array apart in to a 2d array, sort it, then rejoin it. You would be correct in that this is not the most efficient method, though one thing to consider is how often this needs to run and how efficient it really needs to be given the context. I think a 2d array of only 100 elements is not too big of an issue to use an inefficient sort method?
Maybe someone else can come up with better/cleaner methods. =b
Below is code for what I mentioned.
Second attempt (Note: I did not test this one fully, but it seemed to work):
import flash.geom.Point;
var unsortedMap:Array = [
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
];
var sortedMap:Array = [
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
];
var rowSize:int = unsortedMap[0].length;
var rowCount:int = unsortedMap.length;
// points to the last index at start, since want to have filled items at the 'end'
var emptySpotPointer:Point = new Point(rowSize - 1, rowCount - 1);
var fullSpotPointer:Point = new Point(rowSize - 1, rowCount - 1);
function print2dArray(prefix:String, input:Array):void {
trace(prefix);
for(var row:String in input){
trace("\t"+ input[row]);
}
trace("");
}
function addItem(inputX:int, inputY:int):void {
if(unsortedMap[inputY][inputX] == 1){
trace("addItem() - Already an item here: "+ inputX +", "+ inputY);
return;
}
trace("addItem() - Adding an item: "+ inputX +", "+ inputY);
unsortedMap[inputY][inputX] = 1;
sortedMap[emptySpotPointer.y][emptySpotPointer.x] = 1;
fullSpotPointer.x = emptySpotPointer.x;
fullSpotPointer.y = emptySpotPointer.y;
if(emptySpotPointer.x == 0){
emptySpotPointer.x = rowSize - 1;
emptySpotPointer.y--;
} else {
emptySpotPointer.x--;
}
}
function removeItem(inputX:int, inputY:int):void {
if(unsortedMap[inputY][inputX] == 0){
trace("removeItem() - No item here to remove: "+ inputX +", "+ inputY);
return;
}
trace("removeItem() - Removing an item here: "+ inputX +", "+ inputY);
unsortedMap[inputY][inputX] = 0;
sortedMap[fullSpotPointer.y][fullSpotPointer.x] = 0;
if(fullSpotPointer.x == (rowSize - 1)){
fullSpotPointer.x = 0;
fullSpotPointer.y++;
} else {
fullSpotPointer.x++;
}
}
// testing stuff here
// -----------------------------------------------------------------
function addRandomitems():void {
var randomX:int = Math.floor(Math.random() * rowSize);
var randomY:int = Math.floor(Math.random() * rowCount);
addItem(randomX, randomY);
}
function removeRandomitems():void {
var randomX:int = Math.floor(Math.random() * rowSize);
var randomY:int = Math.floor(Math.random() * rowCount);
removeItem(randomX, randomY);
}
print2dArray("unsortedMap", unsortedMap);
print2dArray("sortedMap", sortedMap);
trace("Test: Adding items now ---------------------");
var counter:int = 0;
for(counter = 0; counter < 50; counter++){
addRandomitems();
print2dArray("unsortedMap", unsortedMap);
print2dArray("sortedMap", sortedMap);
}
trace("Test: Removing items now ---------------------");
for(counter = 0; counter < 50; counter++){
removeRandomitems();
print2dArray("unsortedMap", unsortedMap);
print2dArray("sortedMap", sortedMap);
}
Third attempt:
import flash.utils.getTimer;
var myMap:Array = [
[1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0],
[1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0],
[1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0],
[1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0],
[1,0,0,0,0,0,1,1,0,1,1,0,0,1,1,0,0,0,0,0]
];
function sort(inputArray:Array):Array {
var rowSize:int = inputArray[0].length;
var flat:Array = new Array();
// flattening the array here
for(var row:String in inputArray){
flat = flat.concat(inputArray[row]);
}
flat.sort();
var result:Array = new Array();
// recreating the original array here by cutting out 'rowSize' chunks and reforming the 2d array
while(flat.length > 0){
result.push(new Array(flat.splice(0, rowSize)));
}
return result;
}
// testing
var startTimer:int = flash.utils.getTimer();
for(var counter:int = 0; counter < 10000; counter++){
sort(myMap);
}
// Running the above 10,000 times takes 1836ms for me.
trace(flash.utils.getTimer() - startTimer);
// just to see the output:
trace(sort(myMap).join("\n"));

As3 - Script Error on Arrays

please inspect me coding:
function createRandomList():void
{
var newlist:Array = [0,1,2];
var curlist:Array = item[selectedlevel - 1] //selectedlevel = 1;
var normal:int = curlist[0];
var tempboo1:Boolean = false;
var tempboo2:Boolean = false;
var tempboo3:Boolean = false;
while (curlist[0] + curlist[1] + curlist[2] > 0)
{
if (Number(curlist[0]) == 0 && tempboo1 == false)
{
newlist.splice(newlist.indexOf(0), 1);
tempboo1 = true;
}
if (Number(curlist[1]) == 0 && tempboo2 == false)
{
newlist.splice(newlist.indexOf(1), 1);
tempboo2 = true;
}
if (Number(curlist[2]) == 0 && tempboo3 == false)
{
newlist.splice(newlist.indexOf(2), 1);
tempboo3 = true;
}
var temp:int = Math.floor(Math.random()*(newlist.length));
curlist[temp] -= 1;
generatedlist.push(Number(newlist[temp]));
trace(item);
}
while (normal > 0)
{
var temp2:int = Math.floor(Math.random() * 3) + 1;
generatednormal.push(Number(temp2));
normal--;
}
}
My item was [[5,0,0],[10,0,0]];
But after became [[0,0,0],[0,0,0]];
I just want to duplicate Array item to be a new variable curlist.
Every time it traces, returning item[0][0] decreasing 1, I only want to use curlist as a temp Array to calculate a new random Array based on item[0].
Ouput:
4,0,0,10,0,0
3,0,0,10,0,0
2,0,0,10,0,0
1,0,0,10,0,0
0,0,0,10,0,0
Is there any links between them, or is it my problem? Please help! If you need any more infoemation, please comment me!
Arrays are passed by reference, not value. That means when you modify an array through any property that points to it, the original array will be modified.
To make a duplicate, you can use .slice()
Returns a new array that consists of a range of elements from the original array, without modifying the original array. The returned array includes the startIndex element and all elements up to, but not including, the endIndex element.
If you don't pass any parameters, the new array is a duplicate (shallow clone) of the original array.
You can clone your arrays if you want to create a new reference.
function clone( source:Object ):*
{
var myBA:ByteArray = new ByteArray();
myBA.writeObject( source );
myBA.position = 0;
return( myBA.readObject() );
}
var a:Array = [[0,0],[1,1]];
var b:Array = clone(a);
b[0] = [2,2];
trace(a)
trace(b)
Output
0,0,1,1
2,2,1,1
It works for any object, not only arrays.
More infos here : AS3 - Clone an object
var array : Array = [ 1, 2, 3];
var array2 : Array = array.concnt();
array[ 0 ] = 4;
trace( array );// 1, 2, 3
trace( array 2);// 4, 2 ,3
So use .concat() to duplicate an array with primitives. If you have an arrays with arrays. Duplicate the children arrays, and put them into an empty one. If you have children of children arrays and so forth, make something recursive.

AS3 - Check results from an array and add to a counter

I am trying to make a video quiz and when you click it adds a value of 1 to an array. The array size goes up to [9] and I am trying to read from the array so that if there is a value of 1 in the array between [0] and [9] it will add 1 to the counter.
I have got it working for just the first value in the array so I know it works, but I am not sure about how to make it read from all of the array to check for values of 1.
Heres my code so far:
if (clickTimes[0] == 1)
{
counter2++;
}
Better yet, use a for each...in loop. It's really no different from a regular for loop, but it does provide some synactic sugar.
for each (var i:int in clickTimes)
{
if (i == 1)
{
counter2++;
}
}
For the beauty of it...
counter2 = clickTimes.join('').replace(/0/g, '').length;
It puts all your values in one string, remove the zeros and counts the characters left.
var clickTimes:Array = [1,1,1,0,0,0,1,1,0] // 5 1's and 4 0's
var counter2:int = 0
clickTimes.forEach(function(obj:*){
if (obj == 1){
counter2++;
}
})
trace(counter2) // traces 5
What about using a loop?
for (var i:int = 0; i < clickTimes.length; ++i)
{
if (clickTimes[i] == 1)
counter2++;
}
This would increment counter2, for each element that has a value of 1, in the clickTimes array.
You do not need if. Just the following:
var clickTimes:Array = [1,1,1,0,0,0,1,1,0] // 5 1's and 4 0's
var counter2:int = 0;
for each (var i:int in clickTimes)
{
counter2 += i;
}
trace(counter2); //5

Loop through array, set property of each element?

Okay, very simple: there is an array containing 3 objects. Each object has a unique property called "ID" with values of either 1, 2, or 3.
One of the objects gets deleted.
The objective now is to update the ID property of each object corresponding to the new array.length value.
So for example, the object with ID of 2 got deleted. The remaining objects in the array would each have ID values of 1 and 3 respectively.
So the objective is to loop through the array and update the ID properties to 1, and 2 (instead of 1 and 3).
So I guess the question is how to write a loop to update a common property of each element in an array. Thanks.
You can use a for-loop to go through the array, as in walkietokyo's answer, or you can use a method closure:
myArray.forEach ( function ( item:*, i:int, arr:Array) : void { item.ID = i; } );
or a while-loop:
var i:int = -1;
while (++i < myArray.length) myArray[i].ID = i;
for (var i:uint = 1; i <= myArray.length; i++) {
myArray[i].ID = i;
}
General info on loops:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7fcf.html
var i:uint; // for speed keep out of the loop
var arrayLength = myArray.length // for speed keep out of the loop
for (i = 0; i < arrayLength; i++) {
myArray[i].ID = i;
}