473,490 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how define array of array?

Hello,

This may be the answer to a problem I have - or it may not be
possible?!

Can I have initial values of variable situation and count defined as

var situation = 0;
var count = 0;

when during the app situation will vary from 0 to 7 and count from 0
to 8,

and have

value[situation][count] ?

If yes, how do I define the array if arrays?

Thanks

Geoff
Sep 14 '05 #1
20 78036
In Javascript Arrays are "flat", so to make multiple dimensions, you
create an Array as an item of another Array, thus:-

// AN ARRAY CAN BE AN ELEMENT OF ANOTHER ARRAY
var value=new Array();
value[0]=new Array();
value[0][0]="value00";
value[0][1]="value01";
value[1]=new Array();
value[1][0]="value10";
value[1][1]="value11";
alert (value[0][1]);
// SHORTHAND JSON NOTATION
var value=[]; // SAME AS var value=new Array

// EQUIVALENT TO THE ABOVE
var value=[ ["value00","value01"] , ["value10","value11"] ];
alert (value[0][1]);

Sep 14 '05 #2
"Geoff Cox" <ge*******@notquitecorrectfreeuk.com> wrote in message
news:ti********************************@4ax.com...
Hello,

This may be the answer to a problem I have - or it may not be
possible?!

Can I have initial values of variable situation and count defined as

var situation = 0;
var count = 0;

when during the app situation will vary from 0 to 7 and count from 0
to 8,

and have

value[situation][count] ?

If yes, how do I define the array if arrays?

Thanks


var value = new Array(8);
for (var n = 0; n < value.length; n++ ) {
value[n] = new Array(9);
}

--
Dag.
Sep 14 '05 #3
On Wed, 14 Sep 2005 08:34:40 GMT, "Dag Sunde" <me@dagsunde.com> wrote:

var value = new Array(8);
for (var n = 0; n < value.length; n++ ) {
value[n] = new Array(9);
}


Dag,

Thanks for above - it has helped me to get the app working but I am
not quite clear how yet?!

The actual code I use is

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array(8);
}

You will see I use Array(8) rather than Array(9).

The array I am using to collect some slider values, is

slider_value[situation_number][count]

where situation_number varies between 0 and 6

and

count varies between 0 and 7.

Does the defination of the array or arrays above fit in with this?

Cheers

Geoff
Sep 14 '05 #4
On 14 Sep 2005 01:29:17 -0700, "Baconbutty" <ju****@baconbutty.com>
wrote:
In Javascript Arrays are "flat", so to make multiple dimensions, you
create an Array as an item of another Array, thus:-

// AN ARRAY CAN BE AN ELEMENT OF ANOTHER ARRAY
var value=new Array();
value[0]=new Array();
value[0][0]="value00";
value[0][1]="value01";
value[1]=new Array();
value[1][0]="value10";
value[1][1]="value11";
alert (value[0][1]);
// SHORTHAND JSON NOTATION
var value=[]; // SAME AS var value=new Array

// EQUIVALENT TO THE ABOVE
var value=[ ["value00","value01"] , ["value10","value11"] ];
alert (value[0][1]);


Thanks for this - perhaps you could see my last posting to Dag and
comment?

Cheers

Geoff

Sep 14 '05 #5
Geoff Cox said the following on 9/14/2005 5:14 AM:
On Wed, 14 Sep 2005 08:34:40 GMT, "Dag Sunde" <me@dagsunde.com> wrote:
var value = new Array(8);
for (var n = 0; n < value.length; n++ ) {
value[n] = new Array(9);
}

Dag,

Thanks for above - it has helped me to get the app working but I am
not quite clear how yet?!

The actual code I use is

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array(8);
}

You will see I use Array(8) rather than Array(9).

The array I am using to collect some slider values, is

slider_value[situation_number][count]

where situation_number varies between 0 and 6

and

count varies between 0 and 7.

Does the defination of the array or arrays above fit in with this?


It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 14 '05 #6
"Randy Webb" <Hi************@aol.com> wrote in message
news:LY********************@comcast.com...
Geoff Cox said the following on 9/14/2005 5:14 AM:
On Wed, 14 Sep 2005 08:34:40 GMT, "Dag Sunde" <me@dagsunde.com> wrote:
var value = new Array(8);
for (var n = 0; n < value.length; n++ ) {
value[n] = new Array(9);
}

Dag,

Thanks for above - it has helped me to get the app working but I am
not quite clear how yet?!

The actual code I use is

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array(8);
}

You will see I use Array(8) rather than Array(9).

The array I am using to collect some slider values, is

slider_value[situation_number][count]

where situation_number varies between 0 and 6

and

count varies between 0 and 7.

Does the defination of the array or arrays above fit in with this?


It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().

As Randy say, Javascript doesn't need to be told the size of an array.
As long as the variable is declared as an array, it will create an element
as soon as you refer to it, if it isn't created already.

The reason I use it is twofold; I find it (in most situations) clearer
to read my code when I explicitly define the size (If applicable).
The second reason is that my workday consists of using several other
languages in addition to JavaScript that *do* require the size of arrays...

--
Dag.
Sep 14 '05 #7
Arrays do not need to have the length specified. The "length" will be
one more than the last integer index (i.e. counting index position 0).

Thus:-

var a=new Array();
a[10]="Item11";
alert(a.length); // 11 even though 0 - 9 are undefined

Hence what is important is populating those arrays, not declaring them,
as it is only when you populate them that items are effectively
created.

If you search this group for words such as Array, Hash, [[PUT]] and
writings from those such as Richard Cornford, Michael Winter etc there
is a lot of technical information (and argument) about the ECMA Array
object which you may find helpful.

Sep 14 '05 #8
On 14 Sep 2005 04:41:50 -0700, "Baconbutty" <ju****@baconbutty.com>
wrote:
Arrays do not need to have the length specified. The "length" will be
one more than the last integer index (i.e. counting index position 0).

Thus:-

var a=new Array();
a[10]="Item11";
alert(a.length); // 11 even though 0 - 9 are undefined

Hence what is important is populating those arrays, not declaring them,
as it is only when you populate them that items are effectively
created.

If you search this group for words such as Array, Hash, [[PUT]] and
writings from those such as Richard Cornford, Michael Winter etc there
is a lot of technical information (and argument) about the ECMA Array
object which you may find helpful.


Thanks for above and text from others ..

Cheers

Geoff

Sep 14 '05 #9
Dag Sunde wrote:
[...]

As Randy say, Javascript doesn't need to be told the size of an array.
As long as the variable is declared as an array, it will create an element
as soon as you refer to it, if it isn't created already.

The reason I use it is twofold; I find it (in most situations) clearer
to read my code when I explicitly define the size (If applicable).
The second reason is that my workday consists of using several other
languages in addition to JavaScript that *do* require the size of arrays...

To round this out it's worth mentioning that creating an array using:

var someNames = ['fred', 'sue', 'dave'];

is called an 'object intitializer' or 'literal notation', it's
essentially the same as creating the array by calling the constructor
function and then populating it:

var someNames = new Array(3);
someNames[0] = 'fred';
someNames[1] = 'sue';
someNames[2] = 'dave';

<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Creating_New_Objects:Usi ng_Object_Initializers>
Using a length in the constructor is redundant and could create
problems that don't need to be created (I'm thinking of lazy cut 'n
paste developers who leave undefined elements in an array and don't
properly deal with them).

Initializers seem to save a lot of typing, even if they are only used
to declare an empty array:

var x = [];

versus:

var x = new Array()


--
Rob
Sep 14 '05 #10
Randy Webb wrote:


It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().


Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8
Mick
Sep 14 '05 #11
Mick White said the following on 9/14/2005 5:35 PM:
Randy Webb wrote:


It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().


Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8


How is that logical?

If there is a number present in the Array(), then it is the length of an
empty array that is created.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 14 '05 #12
Randy Webb wrote:
Mick White said the following on 9/14/2005 5:35 PM:
Randy Webb wrote:


It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().


Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8

How is that logical?

If there is a number present in the Array(), then it is the length of an
empty array that is created.

What about: var a=[8] ?
And are you sure that every js version follows this rule?
Mick
Sep 14 '05 #13
Randy Webb wrote:
Mick White said the following on 9/14/2005 5:35 PM:
Randy Webb wrote: [...]


Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8

How is that logical?

If there is a number present in the Array(), then it is the length of an
empty array that is created.


http://www.devguru.com/Technologies/...ref/array.html
<<If you specify 'language="Javascript1.2"' in the <SCRIPT>
tag and use a single numeric parameter with the Array constructor,
it will be seen as the value of a single element of the array
rather than the number of elements you want that array to
contain.>>
Mick

Sep 14 '05 #14
ASM
Mick White wrote:
Randy Webb wrote:


It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().


Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8


What logical in conveniences to code ?

a = new Array(11); ==> new array with 11 elements
a[0] = 11; ==> its 1st element value is '11'

i = new Image(420,300) ==> new image 420px width and 300px height
(it is not the value of image neither its name)

I never have understood interest to give this length while setting a new array
when we know we can change it at any time

--
Stephane Moriaux et son [moins] vieux Mac
Sep 14 '05 #15
ASM
Mick White wrote:
http://www.devguru.com/Technologies/...ref/array.html
<<If you specify 'language="Javascript1.2"' in the <SCRIPT> tag and use
a single numeric parameter with the Array constructor, it will be
seen as the value of a single element of the array rather
than the number of elements you want that array to contain.>>
Mick


Mais ils m'en inventent tous les jours !

ouf! it was not specified !
--
Stephane Moriaux et son [moins] vieux Mac
Sep 14 '05 #16
Mick White said the following on 9/14/2005 6:14 PM:
Randy Webb wrote:
Mick White said the following on 9/14/2005 5:35 PM:
Randy Webb wrote:

It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().
Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8


How is that logical?

If there is a number present in the Array(), then it is the length of
an empty array that is created.

What about: var a=[8] ?
And are you sure that every js version follows this rule?


Can you name one that doesn't?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 15 '05 #17
Mick White said the following on 9/14/2005 6:19 PM:
Randy Webb wrote:
Mick White said the following on 9/14/2005 5:35 PM:
Randy Webb wrote:
[...]
Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8


How is that logical?

If there is a number present in the Array(), then it is the length of
an empty array that is created.

http://www.devguru.com/Technologies/...ref/array.html
<<If you specify 'language="Javascript1.2"' in the <SCRIPT> tag and use
a single numeric parameter with the Array constructor, it will be
seen as the value of a single element of the array rather
than the number of elements you want that array to contain.>>


And that is part of why the language attribute is deprecated. But in
javascript1.2, the = is a comparison as well:

y=2

if (y=3){
alert('true')
}

You will get the alert in modern browsers, in NN4, you get no alert
because it is comparing whereas other browsers are saying "Yes, you can
assign 3 to y so its true).

So, using language="javascript1.2" is hardly a good defense.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 15 '05 #18
Mick White <mw***********@rochester.rr.com> writes:

[new Array(8) not equivalent to [8]...]
And are you sure that every js version follows this rule?


No, but we are not talking about every javascript function, but about
Array, which has a long and glorious history of always treating a single
number argument as a the length, and everything else as elements, of
the created array.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 15 '05 #19
Lasse Reichstein Nielsen wrote:
Mick White <mw***********@rochester.rr.com> writes:

[new Array(8) not equivalent to [8]...]
And are you sure that every js version follows this rule?

No, but we are not talking about every javascript function, but about
Array, which has a long and glorious history of always treating a single
number argument as a the length, and everything else as elements, of
the created array.


I don't like it, though...
Mick
Sep 15 '05 #20
Mick White <mw***********@rochester.rr.com> writes:
Lasse Reichstein Nielsen wrote:
Mick White <mw***********@rochester.rr.com> writes:
[new Array(8) equals [,,,,,,,,]]
I don't like it, though...


Me neither. It's an unnecessary exception, and that always invites
bugs. But you can't fight history, and Array has been like that since
.... I guess Netscape 3.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 15 '05 #21

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

Similar topics

3
1845
by: Nicolas Fleury | last post by:
Hi, Does anyone know if arrays would be picklable in python 2.4? Until then, I tried to derive from array.array and add __setstate__ and __getstate__ with the following code, but it seems I'm not...
2
2039
by: Gus Tabares | last post by:
Hello all, I'm trying to subclass array.array but having problems with a default parameter in the init constructor. Example: import array class TestClass(array.array): def __init__(self,...
0
2815
by: Chris Lambacher | last post by:
Hi, I have to do some data manipulation that needs to be fast. I had a generator approach (that was faster than a list approch) which was taking approximately 5 seconds to run both encode and...
2
1464
by: John Machin | last post by:
Googling for "pickle array" in comp.lang.python yields old messages that show a PickleError -- plus one message where Alex Martelli writes "I am but an egg" :O) Looks like arrays are NOW (2.4.1)...
7
38531
by: Michael | last post by:
Hi all, I have got a simple question about the #define command: Can I use it for array initialisation? E.g. #define ARRAYINIT { {0, 1}, {2,3} }
7
29257
by: Roman Mashak | last post by:
Hello, All! I wonder is it possible to define an array containing strings, not single characters? What I want is array 'table' that will have N elements, and every element is a strings tailoring...
5
11378
by: Stuart Norris | last post by:
Dear Readers, I am attempting to define an array of IPAddress-es in C#. I wish to have a array of address so I can try in order to connect to them in a loop to handle unavailable hosts. ...
9
6085
by: Sarfaraz Khan | last post by:
Hello, anyone can guid me how we can handle and define control array in vb.net? Thanks & regards SARFARAZ KHAN
8
7455
by: Susan Bricker | last post by:
Hi. I would like to create an array and use the data to populate some new records when the user clicks on a button (that's the short description). If I were to code the array in C (I know ......
1
2289
by: Fabio | last post by:
Hi All, I have a question concerning the use of array.array inside of C++ code I wrote. I am working with _big_ data files but some entries in these files are usually bounded say between -5 to...
0
7112
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
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
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,...
1
6852
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...
0
7356
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...
1
4878
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...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
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...

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.