Incrementing a Multi-Dimensional Array

C

Chris S.

All,

I'm kind of at a lose this is my first foray into using a multi-dimensional
array and I've run into a snag. for some reason the second dimension always
seems to end up being the last value of the second dimension of the array
ie: in the case below the conditionArray[[0][1]] always ends up being 6.0
where it should be 1.0.



If someone could look at this and tell me what I'm doing wrong I'd be
grateful


Chris S.

var tmp = new Array();

tmp[0] = "Value 1 - 1.0";
tmp[1] = "Value 2 - 2.0";
tmp[2] = "Value 3 - 3.0";
tmp[3] = "Value 4 - 4.0";
tmp[4] = "Value 5 - 5.0";
tmp[5] = "Value 6 - 6.0";

var conditionArray = new Array();
var conditionArrayPosition = 0;

for(var i = 0;i < tmp.length;i++){
var x = tmp.split(" - ");

conditionArray[[conditionArrayPosition][0]] = x[0];
conditionArray[[conditionArrayPosition][1]] = x[1];
conditionArrayPosition++;
}

document.getElementById('test').innerHTML = conditionArray[[2][0]] + " --
" + conditionArray[[0][1]];
 
L

Lee

Chris S. said:
All,

I'm kind of at a lose this is my first foray into using a multi-dimensional
array and I've run into a snag. for some reason the second dimension always
seems to end up being the last value of the second dimension of the array
ie: in the case below the conditionArray[[0][1]] always ends up being 6.0
where it should be 1.0.



If someone could look at this and tell me what I'm doing wrong I'd be
grateful


Chris S.

var tmp = new Array();

tmp[0] = "Value 1 - 1.0";
tmp[1] = "Value 2 - 2.0";
tmp[2] = "Value 3 - 3.0";
tmp[3] = "Value 4 - 4.0";
tmp[4] = "Value 5 - 5.0";
tmp[5] = "Value 6 - 6.0";

var conditionArray = new Array();
var conditionArrayPosition = 0;

for(var i = 0;i < tmp.length;i++){
var x = tmp.split(" - ");

conditionArray[[conditionArrayPosition][0]] = x[0];
conditionArray[[conditionArrayPosition][1]] = x[1];
conditionArrayPosition++;
}

document.getElementById('test').innerHTML = conditionArray[[2][0]] + " --
" + conditionArray[[0][1]];



1. Your notation is wrong.
conditionArray[[0][1]] should be conditionArray[0][1]

2. You're making it more complicated than it should be:

var conditionArray = new Array();

for(var i = 0;i < tmp.length;i++){
conditionArray=tmp.split(" - ");
}
 
C

CES

Lee,

That unfortanatly that dosent work, if you remove the first set of [], leaving array[0][0], the array fails totaly. every referance that I've seen for a 2D Array requires outer brackets and inner brackets as follows array[[a]] - Chris
Chris S. said:
All,

I'm kind of at a lose this is my first foray into using a multi-dimensional
array and I've run into a snag. for some reason the second dimension always
seems to end up being the last value of the second dimension of the array
ie: in the case below the conditionArray[[0][1]] always ends up being 6.0
where it should be 1.0.



If someone could look at this and tell me what I'm doing wrong I'd be
grateful


Chris S.

var tmp = new Array();

tmp[0] = "Value 1 - 1.0";
tmp[1] = "Value 2 - 2.0";
tmp[2] = "Value 3 - 3.0";
tmp[3] = "Value 4 - 4.0";
tmp[4] = "Value 5 - 5.0";
tmp[5] = "Value 6 - 6.0";

var conditionArray = new Array();
var conditionArrayPosition = 0;

for(var i = 0;i < tmp.length;i++){
var x = tmp.split(" - ");

conditionArray[[conditionArrayPosition][0]] = x[0];
conditionArray[[conditionArrayPosition][1]] = x[1];
conditionArrayPosition++;
}

document.getElementById('test').innerHTML = conditionArray[[2][0]] + " --
" + conditionArray[[0][1]];




1. Your notation is wrong.
conditionArray[[0][1]] should be conditionArray[0][1]

2. You're making it more complicated than it should be:

var conditionArray = new Array();

for(var i = 0;i < tmp.length;i++){
conditionArray=tmp.split(" - ");
}
 
V

VK

every referance that I've seen for a 2D Array requires
outer brackets and inner brackets as follows array[[a]]


You must be confused between *instantiating* nested arrays and
*addressing* an array element. Go through this working sample, then
visit <http://www.geocities.com/schools_ring/ArrayAndHash.html>.


<html>
<head>
<title>Array test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script>
var tmp = [
"Value 1 - 1.0",
"Value 2 - 2.0",
"Value 3 - 3.0",
"Value 4 - 4.0",
"Value 5 - 5.0",
"Value 6 - 6.0"
];

var conditionArray = [];

for(var i = 0; i<tmp.length; i++){
conditionArray = tmp.split(" - ");
}

var out = document.getElementById('test');

/* Using ID and innerHTML before onload is a bad idea
because the document is not fully rendered yet
*/

alert(conditionArray[0][0] + ' - ' + conditionArray[0][1]);

</script>

</head>

<body>
<p>Array test</p>
</body>
</html>
 
L

Lee

CES said:
Lee,

That unfortanatly that dosent work, if you remove the first set of [], leaving
array[0][0], the array fails totaly. every referance that I've seen for a 2D
Array requires outer brackets and inner brackets as follows array[[a]] -
Chris


You must be looking at some very bad references.
What does "fails totally" mean?
What error message do you see?
What doesn't happen that you believe should happen?

Please post responses after quoted text in this newsgroup.
 
M

Michael Winter

That unfortanatly that dosent work, if you remove the first set of [],
leaving array[0][0], the array fails totaly.

Of course it will as only array (conditionArray in your code) is an
Array object. All of its elements are undefined so there are no
properties to access.

Lee's replacement is the best, but taking your approach you could use:

var condition = new Array();

for(var i = 0, n = tmp.length, x; i < n; ++i) {
x = tmp.split(' - ');

condition = new Array();
condition[0] = x[0];
condition[1] = x[1];
}

That could improved somewhat using array literals:

x = tmp.split(' - ');

condition = [x[0], x[1]];

Still, it's a gross implementation.


Why your code didn't cause an error is because the first inner set of
brackets created an array (an array literal, as shown above). The second
set then accessed a property of that array, and used the value as the
property name for the original array. That is,

conditionArray[[conditionArrayPosition][0]] = x[0];
conditionArray[[conditionArrayPosition][1]] = x[1];

is equivalent to:

var tempArray;

tempArray = new Array();
tempArray[0] = conditionArrayPosition;

conditionArray[tempArray[0]] = x[0]; /* index value:
* conditionArrayPosition
*/

tempArray = new Array();
tempArray[0] = conditionArrayPosition;

conditionArray[tempArray[1]] = x[1]; /* index value:
* undefined
*/
every referance that I've seen for a 2D Array requires outer brackets
and inner brackets as follows array[[a]] - Chris


If that really is the case, I suggest you /never/ use those references
ever again.

Mike


Please don't top-post - placing your reply above everything else - to
this group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top