...

View Full Version : How do i create a 2d array with loops?



SpiritualStorms
10-31-2004, 01:49 PM
If i wanted to basically create a 2d array with 2 loops, how would i do it? I thought the following would work, but somehow it keeps telling me my array is either null, or not an object:


var field = new Array();

function numberCells() {

var theTable=document.all.oTable;

for(var i = 0; i < theTable.rows.length;i++){
field[i]=theTable.rows(i).length;
for(var j=0;j< theTable.rows(i).cells.length; j ++) {
field[i][j]+=theTable.rows(i).cells(j).length;
}
}
alert(field[i][j])
}

When i run the code, it says basically that the line in blue is null.

AaronW
10-31-2004, 02:01 PM
var field = new Array ();

function numberCells ()
{
var theTable = document.all.oTable;

for (var i = 0; i < theTable.rows.length; i++)
{
field[i] = theTable.rows(i).length;
for (var j = 0; j < theTable.rows(i).cells.length; j++)
{
if (j = 0) field[i] = new Array ();
field[i][j] += theTable.rows(i).cells(j).length;
}
}
alert (field[i][j]);
}

You have to tell the fields[i] to be an array.

SpiritualStorms
10-31-2004, 02:05 PM
I will be damn. I sort of knew, but wasnt totally certain. Makes sense though, since the first array only creates one row, as it were, but for each row, you must specify depth, or columns, and this requires that you create an array for the first index. A bit abstract though.

Thankz a bill Aaron.

SpiritualStorms
10-31-2004, 02:16 PM
Out of curiousity, why the testing of the j variable in your code?


var field = new Array ();

function numberCells ()
{
var theTable = document.all.oTable;

for (var i = 0; i < theTable.rows.length; i++)
{
field[i] = theTable.rows(i).length;
for (var j = 0; j < theTable.rows(i).cells.length; j++)
{
if (j = 0) field[i] = new Array ();
field[i][j] += theTable.rows(i).cells(j).length;
}
}
alert (field[i][j]);
}

Couldnt have i just as easily done something like, "if (theTable) field[i] = new Array ();"?

SpiritualStorms
10-31-2004, 02:26 PM
LOL...I just ran the code, and no green lights. LOL. I dont get it. The same message is popping up. field[..] is null, or not an object.

AaronW
10-31-2004, 02:32 PM
Not sure about your errors, will check that in a second, but the testing on j is so that you tell field[i] to be an array only once. To clean the code up, I've moved it into the for () loop's first argument:



var field = new Array ();

function numberCells ()
{
var theTable = document.all.oTable;

for (var i = 0; i < theTable.rows.length;i++)
{
field[i] = theTable.rows(i).length;
for (var j = 0, field[i] = new Array (); j < theTable.rows(i).cells.length; j++)
field[i][j] += theTable.rows(i).cells(j).length;
}
alert (field[i][j]);
}

AaronW
10-31-2004, 02:34 PM
Actually, I'm going to just take a guess: "field[i][j] +="

You're trying to increment a variable that has no value as of yet. Try making it either just "field[i][j] =", or setting a default value for it in the for loop's first argument.

Garadon
11-01-2004, 06:38 AM
var field =null;
function numberCells ()
{
var theTable = document.all.oTable;//this as far as I know only works in IE
field=new Array ();
for (var i = 0; i < theTable.rows.length; i++)
{
field[i]=new Array ();
for (var j = 0; j < theTable.rows(i).cells.length; j++)
{
field[i][j]="";//need to fill in some data here which presumably ain't the length.
}
}
alert (field[i][j]);//j should be undefined here so that alert should fail
}

SpiritualStorms
11-01-2004, 10:21 PM
To Garadon:

I wish i could tell you that your code works, but unfortunately, it does not. It continues to tell me thaf field[] is null, or undefined. It simply will not understand anything.

Garadon
11-02-2004, 06:25 AM
IN
Mozila it fails in the line I said was IE only so thats logical.

IE it fails in the alert line which I also satd it would. in theory i and j should
have vanished and be undefined in that line cause they should only exist within
the for loops and the alert is after those. though alerting j or i at that lvl in IE
gives the values of the arrays dimensions in there respective place plus 1.

SpiritualStorms
11-02-2004, 06:29 AM
well, i wish i knew what to make of what yu said, cause according to one Perl script, i have mozilla, so i really dont know what to think.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum