Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #8557 > unrolled thread

Create a multi-dimensional array

Started byArchos <raul.san@sent.com>
First post2011-11-23 03:04 -0800
Last post2011-11-23 21:33 +0100
Articles 10 — 8 participants

Back to article view | Back to comp.lang.javascript


Contents

  Create a multi-dimensional array Archos <raul.san@sent.com> - 2011-11-23 03:04 -0800
    Re: Create a multi-dimensional array "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-11-23 13:41 +0200
      Re: Create a multi-dimensional array John G Harris <john@nospam.demon.co.uk> - 2011-11-23 16:41 +0000
      Re: Create a multi-dimensional array Dr J R Stockton <reply1147@merlyn.demon.co.uk> - 2011-11-24 18:42 +0000
        Re: Create a multi-dimensional array "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-11-25 01:53 +0200
          Re: Create a multi-dimensional array Gene Wirchenko <genew@ocis.net> - 2011-11-24 17:15 -0800
    Re: Create a multi-dimensional array Erwin Moller <Since_humans_read_this_I_am_spammed_too_much@spamyourself.com> - 2011-11-23 12:46 +0100
      Re: Create a multi-dimensional array Archos <raul.san@sent.com> - 2011-11-23 06:11 -0800
    Re: Create a multi-dimensional array Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-23 18:34 +0000
      Re: Create a multi-dimensional array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-23 21:33 +0100

#8557 — Create a multi-dimensional array

FromArchos <raul.san@sent.com>
Date2011-11-23 03:04 -0800
SubjectCreate a multi-dimensional array
Message-ID<1c993ed8-05a6-43b4-a28f-e9b0ad1c6241@c16g2000pre.googlegroups.com>
To create a two dimensional array I use:

  var a4 = new Array(3); for (i=0; i<3; i++) a4[i]=new Array(5);

In pseudoce: a4 = [3][5]

Then, to create a 3 dim. array, I tried this one:

  var a5 = new Array(2); for (i=0; i<2; i++) a5[i]=new Array(2); for
(i=0; i<2; i++) a5[i]=new Array(2);

a5 = [2][2][2]

but the firebug console shows that it has not been defined the las
dimension

>>> console.log(a5[1][0])
undefined

[toc] | [next] | [standalone]


#8558

From"Jukka K. Korpela" <jkorpela@cs.tut.fi>
Date2011-11-23 13:41 +0200
Message-ID<jaim56$151$1@dont-email.me>
In reply to#8557
2011-11-23 13:04, Archos wrote:

> To create a two dimensional array I use:
>
>    var a4 = new Array(3); for (i=0; i<3; i++) a4[i]=new Array(5);
>
> In pseudoce: a4 = [3][5]

The value of a4 is an array, where the elements are arrays. Calling it a 
two-dimensional array probably confuses rather than helps. The notation 
[3][5] is not meaningful as such.

> Then, to create a 3 dim. array, I tried this one:
>
>    var a5 = new Array(2); for (i=0; i<2; i++) a5[i]=new Array(2); for
> (i=0; i<2; i++) a5[i]=new Array(2);

I don't see what you regard as three-dimensional here, and I don't 
understand why you have the same 'for' loop twice.

>>>> console.log(a5[1][0])
> undefined

Naturally, because no value has been assigned to it. You have created an 
array with two elements, each of which is an array of two elements, each 
of which is initially undefined. Try setting e.g.
a5[1] = ['Hello world', 42]
or
a5[1][0] = 'foobar'
and you'll see that a5 works fine as a simulation of a two-dimensional 
array.

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

[toc] | [prev] | [next] | [standalone]


#8570

FromJohn G Harris <john@nospam.demon.co.uk>
Date2011-11-23 16:41 +0000
Message-ID<UCcx2MGLJSzOFwYR@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD>
In reply to#8558
On Wed, 23 Nov 2011 at 13:41:28, in comp.lang.javascript, Jukka K.
Korpela wrote:

  <snip>
>and you'll see that a5 works fine as a simulation of a two-dimensional
>array.

It's more than a 'simulation'. If it's a structure that correctly
implements the job of a two-dimensional array then it *is* a two
dimensional array.

Just because programs are not built into the language doesn't mean that
programs are mere 'simulations'.

  John
-- 
John Harris

[toc] | [prev] | [next] | [standalone]


#8613

FromDr J R Stockton <reply1147@merlyn.demon.co.uk>
Date2011-11-24 18:42 +0000
Message-ID<y6YD3kFVApzOFwPL@invalid.uk.co.demon.merlyn.invalid>
In reply to#8558
In comp.lang.javascript message <jaim56$151$1@dont-email.me>, Wed, 23
Nov 2011 13:41:28, Jukka K. Korpela <jkorpela@cs.tut.fi> posted:

>2011-11-23 13:04, Archos wrote:
>
>> To create a two dimensional array I use:
>>
>>    var a4 = new Array(3); for (i=0; i<3; i++) a4[i]=new Array(5);
>>
>> In pseudoce: a4 = [3][5]
>
>The value of a4 is an array, where the elements are arrays. Calling it
>a two-dimensional array probably confuses rather than helps.

It will help if the reader is accustomed to languages which support
multi-dimensional arrays, and wants in JavaScript the effect of a two-
dimensional array.  Those languages include Algol (IIRC), Pascal/Delphi,
and English.

> The notation [3][5] is not meaningful as such.

The author meant to say that it is pseudocode.  It's certainly
meaningful in some dialects of pseudocode.


>Naturally, because no value has been assigned to it. You have created
>an array with two elements, each of which is an array of two elements,
>each of which is initially undefined. Try setting e.g.
>a5[1] = ['Hello world', 42]
>or
>a5[1][0] = 'foobar'
>and you'll see that a5 works fine as a simulation of a two-dimensional
>array.

Provided that one recalls that a5 must be an array (maybe empty) before
the first of those assignments, and that a5[1] must be an array (maybe
empty) before the second of those assignments.

-- 
 (c) John Stockton, nr London UK  ?@merlyn.demon.co.uk  IE8 FF8 Op11 Sf5 Cr15
   news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
   <http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
   <http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

[toc] | [prev] | [next] | [standalone]


#8614

From"Jukka K. Korpela" <jkorpela@cs.tut.fi>
Date2011-11-25 01:53 +0200
Message-ID<jamlek$sk5$1@dont-email.me>
In reply to#8613
2011-11-24 20:42, Dr J R Stockton wrote:

>> The value of a4 is an array, where the elements are arrays. Calling it
>> a two-dimensional array probably confuses rather than helps.
>
> It will help if the reader is accustomed to languages which support
> multi-dimensional arrays,

Sounds like confusion. Do you really wish to promote such ideas?

>> The notation [3][5] is not meaningful as such.
>
> The author meant to say that it is pseudocode.  It's certainly
> meaningful in some dialects of pseudocode.

Nonsense does not become any more meaningful by someone's calling it 
"pseudocode".

> Provided that one recalls that a5 must be an array (maybe empty) before
> the first of those assignments, and that a5[1] must be an array (maybe
> empty) before the second of those assignments.

Are you trying to make a point, or just noise?

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

[toc] | [prev] | [next] | [standalone]


#8615

FromGene Wirchenko <genew@ocis.net>
Date2011-11-24 17:15 -0800
Message-ID<0vqtc7dtfflfvg9u66s8ugb5aqpkbcv5bn@4ax.com>
In reply to#8614
On Fri, 25 Nov 2011 01:53:58 +0200, "Jukka K. Korpela"
<jkorpela@cs.tut.fi> wrote:

>2011-11-24 20:42, Dr J R Stockton wrote:
>
>>> The value of a4 is an array, where the elements are arrays. Calling it
>>> a two-dimensional array probably confuses rather than helps.
>>
>> It will help if the reader is accustomed to languages which support
>> multi-dimensional arrays,
>
>Sounds like confusion. Do you really wish to promote such ideas?
>
>>> The notation [3][5] is not meaningful as such.
>>
>> The author meant to say that it is pseudocode.  It's certainly
>> meaningful in some dialects of pseudocode.
>
>Nonsense does not become any more meaningful by someone's calling it 
>"pseudocode".

     At least two of us understood it just fine.  Check your language
calibration?

[snip]

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#8559

FromErwin Moller <Since_humans_read_this_I_am_spammed_too_much@spamyourself.com>
Date2011-11-23 12:46 +0100
Message-ID<4eccdd23$0$6851$e4fe514c@news2.news.xs4all.nl>
In reply to#8557
On 11/23/2011 12:04 PM, Archos wrote:
> To create a two dimensional array I use:
>
>    var a4 = new Array(3); for (i=0; i<3; i++) a4[i]=new Array(5);
>
> In pseudoce: a4 = [3][5]
>
> Then, to create a 3 dim. array, I tried this one:
>
>    var a5 = new Array(2); for (i=0; i<2; i++) a5[i]=new Array(2); for
> (i=0; i<2; i++) a5[i]=new Array(2);
>

Hi Archos,

I find it difficult to read your Javascript coding style.
All on 1 line, and no {} to make your intentions clear.

Did you mean to do this?
var a5 = new Array(2);
for (i=0; i<2; i++){
  a5[i]=new Array(2);
  for (i=0; i<2; i++){
   a5[i]=new Array(2);
  }
}

That would be wrong of course.
2 times i, and the second loop also does something strange with the 
indexes of a5.

What you want, however, is probably something like this:

var a5 = new Array(2);
for (var i=0; i<2; i++){
  a5[i]=new Array(2);
  for (var j=0; j<2; j++){
   a5[i][j]=new Array(2);
  }
}

That uses i and j to make things clearer and uses "var".
Now you can say:
a5[1][1][0] = "whatever";


Regards,
Erwin Moller


> a5 = [2][2][2]
>
> but the firebug console shows that it has not been defined the las
> dimension
>
>>>> console.log(a5[1][0])
> undefined


-- 
"That which can be asserted without evidence, can be dismissed without 
evidence."
-- Christopher Hitchens

[toc] | [prev] | [next] | [standalone]


#8567

FromArchos <raul.san@sent.com>
Date2011-11-23 06:11 -0800
Message-ID<9551addb-e6f3-4d13-a2e8-dea454573c53@m7g2000vbc.googlegroups.com>
In reply to#8559
On Nov 23, 11:46 am, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spamyourself.com> wrote:
> What you want, however, is probably something like this:
>
> var a5 = new Array(2);
> for (var i=0; i<2; i++){
>   a5[i]=new Array(2);
>   for (var j=0; j<2; j++){
>    a5[i][j]=new Array(2);
>   }
>
> }
>
> That uses i and j to make things clearer and uses "var".
Could be used "let" instead of "var"? since it's for local scope.

Thanks!

[toc] | [prev] | [next] | [standalone]


#8571

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2011-11-23 18:34 +0000
Message-ID<4ecd3ca5$0$28487$a8266bb1@newsreader.readnews.com>
In reply to#8557
On Wed, 23 Nov 2011 03:04:49 -0800, Archos wrote:

> To create a two dimensional array I use:
> 
>   var a4 = new Array(3); for (i=0; i<3; i++) a4[i]=new Array(5);
> 
> In pseudoce: a4 = [3][5]
> 
> Then, to create a 3 dim. array, I tried this one:
> 
>   var a5 = new Array(2); for (i=0; i<2; i++) a5[i]=new Array(2); for
> (i=0; i<2; i++) a5[i]=new Array(2);
> 
> a5 = [2][2][2]

Yes, because all you've done is define a5[i] twice. I think that what you 
want to do is define a5[i][?] as an array?

Try (untested):

var a5 = new Array(2);
for (var i=0; i<2; i++) 
{
a5[i]=new Array(2); 
for (var j=0; j<2; j++) a5[i][j]=new Array(2);
}

Rgds

Denis McMahon

[toc] | [prev] | [next] | [standalone]


#8572

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2011-11-23 21:33 +0100
Message-ID<3003096.SPkdTlGXAF@PointedEars.de>
In reply to#8571
Denis McMahon wrote:

> var a5 = new Array(2);
> for (var i=0; i<2; i++)
> {
> a5[i]=new Array(2);
> for (var j=0; j<2; j++) a5[i][j]=new Array(2);
> }

Using arguments for the Array constructor is pointless here and error-prone.  
For that matter, using the Array constructor is unnecessary and potentially 
error-prone.  Use the Array initializer instead; it is most certainly 
ubiquitous by now [1]:

  var a5 = [];
  for (var i = 0; i < 2; ++i)
  {
    a5[i] = [];
    for (var j = 0; j < 2; ++j)
    {
      a5[i][j] = [];
    }
  }

Note that you can save memory if you do not create all potentially needed 
Array instances, but you have to pay for that with a bit of runtime (tests 
before access whether there already is an Array instance reference 
assigned).


PointedEars
___________
[1] <http://PointedEars.de/es-matrix/#!>
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
  -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web