473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Incrementing a Multi-Dimensional Array

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[i].split(" - ");

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

document.getElementById('test').innerHTML = conditionArray[[2][0]] + " --
" + conditionArray[[0][1]];
Jul 27 '05 #1
5 1787
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[i].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[i]=tmp[i].split(" - ");
}

Jul 27 '05 #2
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][b]] - 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[i].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[i]=tmp[i].split(" - ");
}

Jul 27 '05 #3
VK
> every referance that I've seen for a 2D Array requires
outer brackets and inner brackets as follows array[[a][b]]


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[i] = tmp[i].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>

Jul 27 '05 #4
On 27/07/2005 22:11, CES wrote:
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[i].split(' - ');

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

That could improved somewhat using array literals:

x = tmp[i].split(' - ');

condition[i] = [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][b]] - 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.

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jul 27 '05 #5
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][b]] -
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.

Jul 27 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tom | last post by:
Anyone help on this? PHP/MySQL I have a repeat region displaying records from orders in a CSV format: CalJoe33,18,08/23/2004,FED EX,PREPAID,WEBSITE,CA,Book,25.95,??? CalJoe33,18,08/23/2004,FED...
2
by: Mike Brearley | last post by:
I have a counter (alright one I found on asp101.com) that checks for a session variable to prevent the counter from incrmenting if a user refreshes the page or returns to the page during the same...
6
by: J. Campbell | last post by:
Hi everyone. I'm sure that this is common knowledge for many/most here. However, it was rather illuminating for me (someone learning c++) and I thought it might be helpful for someone else. I'm...
3
by: Robin Tucker | last post by:
Hi, I'm in the process of implementing a multi-user system containing an adjacency list (tree structure). I'm using a TIMESTAMP field on each record in the adjacency list in order to tell when...
6
by: bigjmt | last post by:
Sorry to bother you guys with what I though would be an easy task. I have a table in my database were I would like one of the rows to increment a number for each row. I want the first row to start...
2
by: brian | last post by:
Hi, before coming to .NET, I utilized regular expressions mostly in JScript / JavaScript and also in my favorite text editor: TextPad (www.textpad.com) I don't know about JScript/JavaScript, but...
10
by: Antanas | last post by:
The problem is that when AddID is used multiple times in the same select statement, it returns the same value in all places. How could I force function AddID to increment OBJECTID sequence? Here...
2
by: Payson Books | last post by:
Where can I find some info on grabbing the recordID of the last record, incrementing it by one, and using the new number in a new record.
26
by: Bas Wassink | last post by:
Hi there, Does the ANSI standard say anything about incrementing variables past their limits ? When I compile code like this: unsigned char x = 255; x++; printf ( "%d\n", x );
53
by: subramanian100in | last post by:
I saw this question from www.brainbench.com void *ptr; myStruct myArray; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr +...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.