473,320 Members | 1,958 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,320 software developers and data experts.

Nested or Multidimensional Arrays

I can't figure out why this doesn't work:

---------------------------------------------
greeting = new Array();

greeting[0][0] = "hey";
greeting[0][1] = "bye";
trace(greeting[0][0] + greeting[0][1]);

---------------------------------------------

Shouldn't this automatically create a multidimensional array? Can someone
help me? Thanks.
Jul 23 '05 #1
5 1916
sorry I forgot to mention that the trace part is because it's something I'm
actually doing in actionscript, but since actionscriptis based on
javascript, I figured I could get a good answer here too.

"TheKeith" <no@spam.com> wrote in message
news:9b********************@giganews.com...
I can't figure out why this doesn't work:

---------------------------------------------
greeting = new Array();

greeting[0][0] = "hey";
greeting[0][1] = "bye";
trace(greeting[0][0] + greeting[0][1]);

---------------------------------------------

Shouldn't this automatically create a multidimensional array? Can someone
help me? Thanks.

Jul 23 '05 #2
On Sat, 3 Apr 2004 15:02:37 -0500, TheKeith <no@spam.com> wrote:
greeting = new Array();

greeting[0][0] = "hey";
greeting[0][1] = "bye";

Shouldn't this automatically create a multidimensional array? [...]


No, it shouldn't.

An array is just an array, and with "= new Array()" or "= []", all of its
elements are undefined. As such, using myArray[ index ] will return
undefined, and attempting to subscript that is simply an error.

You'll have to assign an array to each element of the original array to
make a multi-dimensional array. You could either do this explicitly, or
write two functions; one to set, and the other to get elements at the
given indicies. They could check if the sub-array exists within the first,
and if not, create it before assigning a value to it. Similarly, if the
sub-array doesn't exist when trying to get a value, return undefined but
without causing an error (by not actually accessing a non-existent array).

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #3
TheKeith wrote:
Shouldn't this automatically create a multidimensional array? Can
someone help me? Thanks.


I use this helper function to create (and debug) multi-dimensional arrays.

//====== Multidimensional array

function MDArray (dimensions) {
// instantiate objects representing a multi dimensional array
// dimensions - array of numbers, ith number is nunmber of elements in
dimension i

this.length = dimensions[0]
this.dimensions = dimensions
if (dimensions.length>0) {
for (var i=0;i<this.length;i++) {
this[i] = new MDArray(dimensions.slice(1))
}
}
}
MDArray.prototype = new Array()
MDArray.prototype.constructor = MDArray
MDArray.prototype.toHTML = function(addr) {
var html = ''
if (this.dimensions.length > 2) {
for (var i=0;i<this.dimensions[0];i++) {
html += this[i].toHTML((addr?addr+',':'')+i)
}
}
else
if (this.dimensions.length == 2) {

html += '<BR>'+arguments[0]+'<TABLE BORDER=1>'
for (var i=0;i<this.dimensions[0];i++) { html += '<TR>'
for (var j=0;j<this.dimensions[1];j++) { html += '<TD>'
html += this[i][j] + '</TD>'
} html += '</TR>'
} html += '</TABLE>'
}
else {
html = this.toString()
} return html
}

mda = new MDArray([4,4,4,4])
mda[0][0][0][0] = 0
mda[1][1][1][1] = 1
mda[2][2][2][2] = 2
mda[3][3][3][3] = 4
document.write (mda.toHTML())

--
Richard A. DeVenezia
http://www.devenezia.com/downloads/sas/macros/?m=xmlib
Jul 23 '05 #4

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
On Sat, 3 Apr 2004 15:02:37 -0500, TheKeith <no@spam.com> wrote:
greeting = new Array();

greeting[0][0] = "hey";
greeting[0][1] = "bye";

Shouldn't this automatically create a multidimensional array? [...]


No, it shouldn't.

An array is just an array, and with "= new Array()" or "= []", all of its
elements are undefined. As such, using myArray[ index ] will return
undefined, and attempting to subscript that is simply an error.

You'll have to assign an array to each element of the original array to
make a multi-dimensional array. You could either do this explicitly, or
write two functions; one to set, and the other to get elements at the
given indicies. They could check if the sub-array exists within the first,
and if not, create it before assigning a value to it. Similarly, if the
sub-array doesn't exist when trying to get a value, return undefined but
without causing an error (by not actually accessing a non-existent array).


thanks a lot Mike, I got it working ok now.
Jul 23 '05 #5

"Richard A. DeVenezia" <ra******@ix.netcom.com> wrote in message
news:c4*************@ID-168040.news.uni-berlin.de...
TheKeith wrote:
Shouldn't this automatically create a multidimensional array? Can
someone help me? Thanks.


I use this helper function to create (and debug) multi-dimensional arrays.

//====== Multidimensional array

function MDArray (dimensions) {
// instantiate objects representing a multi dimensional array
// dimensions - array of numbers, ith number is nunmber of elements in
dimension i

this.length = dimensions[0]
this.dimensions = dimensions
if (dimensions.length>0) {
for (var i=0;i<this.length;i++) {
this[i] = new MDArray(dimensions.slice(1))
}
}
}
MDArray.prototype = new Array()
MDArray.prototype.constructor = MDArray
MDArray.prototype.toHTML = function(addr) {
var html = ''
if (this.dimensions.length > 2) {
for (var i=0;i<this.dimensions[0];i++) {
html += this[i].toHTML((addr?addr+',':'')+i)
}
}
else
if (this.dimensions.length == 2) {

html += '<BR>'+arguments[0]+'<TABLE BORDER=1>'
for (var i=0;i<this.dimensions[0];i++) { html += '<TR>'
for (var j=0;j<this.dimensions[1];j++) { html += '<TD>'
html += this[i][j] + '</TD>'
} html += '</TR>'
} html += '</TABLE>'
}
else {
html = this.toString()
} return html
}

mda = new MDArray([4,4,4,4])
mda[0][0][0][0] = 0
mda[1][1][1][1] = 1
mda[2][2][2][2] = 2
mda[3][3][3][3] = 4
document.write (mda.toHTML())

--
Richard A. DeVenezia
http://www.devenezia.com/downloads/sas/macros/?m=xmlib

Thanks a lot, Richard, but it's a little more than I needed to do. I just
realized that I needed to initiate a new instance of the nested array:

array1[1] = new Array();

--that works fine for me.
Jul 23 '05 #6

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

Similar topics

5
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
2
by: Terry | last post by:
Hi, can someone plz tell me how multidimensional arrays (like a 2-D array) are stored in memory? Are they like single dimensional arrays? Stored sequentially in one "row", so to say? Thanks ...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
3
by: Claire | last post by:
I have a multidimensional array defined as private double myArray = new double; The first column of the array contains X values, the other contains Y values I have a charting function defined as...
3
by: Ravi Singh (UCSD) | last post by:
Hello all I am trying to use jagged and multi-dimensional arrays in C++. In C# these work fine // for jagged arrays string jaggedArray = new string ; //for multidimensional arrays string...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
2
by: oopsatwork | last post by:
Ok...so, I have been outside of the C world for a _very_ long time...but not so long as to remember how to do multidimensional arrays. So, let me state that I know how to malloc pointers to...
12
by: filippo nanni | last post by:
Hello everybody, my question is this: I have two multidimensional arrays and I have to create a third one (for later use) from comparing these two. Here is my example code: //BEGIN CODE var...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.