# Unexpected behaviour by two references to single Array - Help Please!

**URL:** <https://forum.kirupa.com/t/unexpected-behaviour-by-two-references-to-single-array-help-please/299278>\
**Category:** flash\
**Created:** [November 2, 2009, 3:46pm UTC](https://forum.kirupa.com/t/unexpected-behaviour-by-two-references-to-single-array-help-please/299278 "2009-11-02T15:46:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shaedo](https://avatars.discourse-cdn.com/v4/letter/s/e19b73/32.png) [@Shaedo](https://forum.kirupa.com/u/Shaedo)\
**Post date:** [November 2, 2009, 3:46pm UTC](https://forum.kirupa.com/t/unexpected-behaviour-by-two-references-to-single-array-help-please/299278/1 "2009-11-02T15:46:46Z")

</div>

Hi,  
The problem is probably quicker explained by just looking at the code (fairly simple).

I have one array. I Have two references to it. When I alter it using the two references this changes the array and tracing the two references. When I use a function to alter the array, tracing the two references to the same array now give different results.

```auto
var ar1:Array=new Array(1,2,3,4,9);

var ar2:Array = ar1;
trace(ar1);//1,2,3,4,9
trace(ar2);//1,2,3,4,9
// .... everything as expected so far

ar1[0] = 4;
trace(ar1);//4,2,3,4,9
trace(ar2);//4,2,3,4,9
// .... everything as expected so far

ar1 = AA(ar1);
trace(ar1);//4,2,3,4
trace(ar2);//4,2,3,4,9
// ... huh ? why are these now different ?

	function AA(a:Array):Array
	{
		var tempArray:Array=new Array;
		for(var i:uint=0;i<a.length;i++)
		{
			if(a* < 6) tempArray.push(a*);
			//if(a*.stage!=null)tempArray.push(a*);//this is what I am actually using this for; to remove objects that are removed from the display list from the array (not just for GC purposes);
		}
		return tempArray;
	}

```

I am not sure why the last two traces are not the same ??

Thank you for your consideration!

As the sailor with the ships wheel down his pants said “This is driving me nuts!”

S.
