473,607 Members | 2,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nested or Multidimensiona l 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 multidimensiona l array? Can someone
help me? Thanks.
Jul 23 '05 #1
5 1939
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******** ************@gi ganews.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 multidimensiona l 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 multidimensiona l 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.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #3
TheKeith wrote:
Shouldn't this automatically create a multidimensiona l array? Can
someone help me? Thanks.


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

//====== Multidimensiona l 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.len gth>0) {
for (var i=0;i<this.leng th;i++) {
this[i] = new MDArray(dimensi ons.slice(1))
}
}
}
MDArray.prototy pe = new Array()
MDArray.prototy pe.constructor = MDArray
MDArray.prototy pe.toHTML = function(addr) {
var html = ''
if (this.dimension s.length > 2) {
for (var i=0;i<this.dime nsions[0];i++) {
html += this[i].toHTML((addr?a ddr+',':'')+i)
}
}
else
if (this.dimension s.length == 2) {

html += '<BR>'+argument s[0]+'<TABLE BORDER=1>'
for (var i=0;i<this.dime nsions[0];i++) { html += '<TR>'
for (var j=0;j<this.dime nsions[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.******@bluey onder.co.invali d> 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 multidimensiona l 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.ne tcom.com> wrote in message
news:c4******** *****@ID-168040.news.uni-berlin.de...
TheKeith wrote:
Shouldn't this automatically create a multidimensiona l array? Can
someone help me? Thanks.


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

//====== Multidimensiona l 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.len gth>0) {
for (var i=0;i<this.leng th;i++) {
this[i] = new MDArray(dimensi ons.slice(1))
}
}
}
MDArray.prototy pe = new Array()
MDArray.prototy pe.constructor = MDArray
MDArray.prototy pe.toHTML = function(addr) {
var html = ''
if (this.dimension s.length > 2) {
for (var i=0;i<this.dime nsions[0];i++) {
html += this[i].toHTML((addr?a ddr+',':'')+i)
}
}
else
if (this.dimension s.length == 2) {

html += '<BR>'+argument s[0]+'<TABLE BORDER=1>'
for (var i=0;i<this.dime nsions[0];i++) { html += '<TR>'
for (var j=0;j<this.dime nsions[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
6743
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 clearly be the most efficient way to do it, but it only works on a copy of the original array and not the original (which is counter intuitive in my estimation). Using each doesn't work consistently either. Not only that, it's unduly complex for...
2
1137
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 Terry
9
6661
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 function (which was translated from Fortran code), among other heinous and numerous declarations, is this bit: static float bbuff; static int bkey; static int buse;
3
8187
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 Add(Array XValues, Array YValues) How do I call the Add function, passing my array columns please. thanks
3
2189
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 multidimensionalArray = new string
10
12196
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 help do the most basic things. One of the "basic" things I haven't been able to find out how to do is how to delete an item from a multidimensional array object and resize it afterwards. It seems so easy to conceive of the code to delete...
2
4477
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 pointers and then malloc my data space. This question is NOT about that. I need to keep a dynamically sized (a result of command line options) matrix in a shared memory segment created via shmget and attached via shmat. My first instinct was to do...
12
5638
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 myGroups = ,,]; var myProducts = ,,,]; var newGroups = new Array();//Resulting Array function main() { for(i=0; i<myGroups.length; i++){
9
4489
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 function: x = new int;
0
8469
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8128
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8322
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6803
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5997
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3953
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4013
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.