...

View Full Version : Array in function



ryan1234
10-10-2012, 12:40 AM
Basically I have a global array which I use in a function and in this function values are assigned to the array depending on what the user enters.

I then need to use the array again with the assigned values, in another function. However I don't believe the assigned values will be in the array because of variable scope.

Is this correct, and if so how do I keep the values within the array after the function ends?

Thanks.

xelawho
10-10-2012, 12:43 AM
if the array is in the global scope it should be available to all functions, and if the array has been changed within one function those changes are to the array itself - scope has nothing to do with it.

have the changes actually occurred, or does it just appear so?

it might be useful to see a sample of your code

Old Pedant
10-10-2012, 05:52 AM
Even if the array is *NOT* global, changes made in a function *WILL* affect its value in the caller. That's because in JavaScript *ALL* objects are passed by reference, and an array *IS* an object.

Here's a clear demo of that:


<script type="text/javascript">
function one( ary1 )
{
ary1[0] = "banana pudding";
ary1[3] = "zamboni";
}
function two( ary2 )
{
alert( ary2.join("\n") );
}
function test()
{
var ary = [ 1, 2, 3 ];
one( ary );
two( ary );
}
test( );
</script>

As you can see, the array ary is *NOT* a global. Yet when we pass it to function one( ) it's first element is changed, as is proven when we then pass the same variable to function two( ).

And, as you can see, we can even add elements to the array and the added elements are carried back to the caller and then to the second callee.

THERE IS ONLY ONE ARRAY in all that code. The names ary, ary1 and ary2 are all actually referencing the same array.

ryan1234
10-10-2012, 11:04 PM
The couple of functions I have are:



function save() {

//for (var i = 0; i <= 8; i++) {
// myArray[i] = new Array(9);
// for (var j = 0; j <= 8; j++) {
// myArray[i][j] = '';
//}
//}

var col, row;
for (row = 0; row <=8; row++) {
for (col=0; col <= 8; col++) {
var cell = document.getElementById('cell_' + col + '_' + row);
if (parseInt(cell.innerHTML)) {
myArray[[col][row]] = cell.innerHTML;
}
}
}
}

function restore() {
var col, row;
for (row = 0; row <=8; row++) {
for (col=0; col <= 8; col++) {
var cell = document.getElementById('cell_' + col + '_' + row);
if (parseInt(cell.innerHTML)) {
document.getElementById('cell_' + col + '_' + row).innerHTML = myArray[[col][row]];
}
else {
document.getElementById('cell_' + col + '_' + row).innerHTML = '';
}
}
}
}


The array is declared earlier in the code.

Basically I use the save() function to save values from a table into the array. However when I don't enter a value into a particular cell (so that bit of array is empty), save it and press restore, it then puts a 6 into the cell.

Any ideas.

Old Pedant
10-11-2012, 12:20 AM
This code is nonsense:


myArray[[col][row]] = cell.innerHTML;

SURELY you actually mean


myArray[col][row] = cell.innerHTML;


But I also don't understand this:


if (parseInt(cell.innerHTML)) {

What are you expecting that to do??? Are you trying to find out if the cell contents are a number? Okay, that works. But better would be to use ! isNaN( )

In any case, though, you don't provide any default value when the if test fails, so you that array element WILL NOT BE DEFINED. Unless, of course, it was defined earlier.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum