Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How the heck is JS processed? 1

Status
Not open for further replies.

KevinFSI

Programmer
Joined
Nov 17, 2000
Messages
582
Location
US
This should be a simple one for the gurus out there. Take a look at the code and then see if you can help me.
Code:
array1[0] = "num1";
array1[1] = "num2";
array2[0] = array1;

array1[0] = "num3";
array1[1] = "num4";
array2[1] = array1;
Ok, the plan was to create a 2D array called array2 where array2[0][0] was "num1" and array2[0][1] was "num2" and so on. Simple enough?

What I didn't realize is that even though I assigned array1 (which was "num1" & "num2") to the first element of array 2 BEFORE moving on, when I was finished, "num3" & "num4" were the only things in array2. The result looked like this:
Code:
array2[0][0] ----- num3
array2[0][1] ----- num4
array2[1][0] ----- num3
array2[1][1] ----- num4
I hope I made sense enough. Now I realize I can't overwrite the same array and assign it to different elements of the second array, but why does it do this? I hope someone knows.

Thanks, Kevin
slanek@ssd.fsi.com
 
You really should be asking this question in the JavaScript forum, not the CF forum.

When you set one array to another, you are only "referencing" the array, not copying it. So when you change array1 to have different values, anthing that is pointed to it will change values as well

The way to fix this is to reset array1 (array1= new Array()).

Put the below sscript inside a body tag and it should reveal the differences.

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
[COLOR=666666]<!--[/color]

[COLOR=666666]// Function to Print out 2D array[/color]
function print2dArray(array, string){
for(i=0; i < array.length; i++)
for(ii=0; ii < array
Code:
[
i
Code:
]
.length; ii++)
document.write(string + &quot;
Code:
[
&quot;
+ i + &quot;
Code:
]
Code:
[
&quot;
+ ii + &quot;
Code:
]
= &quot;
+ array2
Code:
[
i
Code:
]
Code:
[
ii
Code:
]
+ &quot;<BR>&quot;);

document.write(&quot;<BR>&quot;);
}

[COLOR=666666]// Incorrect Method[/color]
array1 = new Array();
array2 = new Array();

array1
Code:
[
0
Code:
]
= &quot;num1&quot;;
array1
Code:
[
1
Code:
]
= &quot;num2&quot;;
array2
Code:
[
0
Code:
]
= array1;

array1
Code:
[
0
Code:
]
= &quot;num3&quot;;
array1
Code:
[
1
Code:
]
= &quot;num4&quot;;
array2
Code:
[
1
Code:
]
= array1;

print2dArray(array2, 'array2')

[COLOR=666666]// Correct Method[/color]
array1 = new Array();
array2 = new Array();

array1
Code:
[
0
Code:
]
= &quot;num1&quot;;
array1
Code:
[
1
Code:
]
= &quot;num2&quot;;
array2
Code:
[
0
Code:
]
= array1;

array1 = new Array();
array1
Code:
[
0
Code:
]
= &quot;num3&quot;;
array1
Code:
[
1
Code:
]
= &quot;num4&quot;;
array2
Code:
[
1
Code:
]
= array1;

print2dArray(array2, 'array2')



[COLOR=666666]//-->[/color]
</SCRIPT> - tleish
 
tleish,
Thanks, that was some really good info. Also, I thought I was in the JS forum. My mistake. Had I been paying attention I would have gone there instead. The CF forum comes up when I open tek-tips, and I was so intent on posting this question, I neglected to look at where I was.

Thanks again! Kevin
slanek@ssd.fsi.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top