Two dimensional array 
Author Message
 Two dimensional array

Hi,

This seems to be a very stupide question, but I just can't create a two
dimensional array in JScript like in VBScript.

What should be those lines of codes in JScript :

Dim arr(1,1)
arr(0,0) = 1
arr(1,0) = 2
arr(0,1) = "a"
arr(1,1) = "b"

Thanks.

Fred



Fri, 07 Feb 2003 03:00:00 GMT  
 Two dimensional array

JScript doesn't support multi-dimensional arrays.  You can fake it with an array of arrays...

--
Michael Harris
Microsoft.MVP.Scripting
--

Hi,

This seems to be a very stupide question, but I just can't create a two
dimensional array in JScript like in VBScript.

What should be those lines of codes in JScript :

Dim arr(1,1)
arr(0,0) = 1
arr(1,0) = 2
arr(0,1) = "a"
arr(1,1) = "b"

Thanks.

Fred



Fri, 07 Feb 2003 03:00:00 GMT  
 Two dimensional array

Quote:

> JScript doesn't support multi-dimensional arrays.  You can fake it with an

array of arrays...

Which essentially is multi-dimensional arrays, you just declare them
differently ;)

--
Thor Larholm


Quote:
> Hi,

> This seems to be a very stupide question, but I just can't create a two
> dimensional array in JScript like in VBScript.

> What should be those lines of codes in JScript :

> Dim arr(1,1)
> arr(0,0) = 1
> arr(1,0) = 2
> arr(0,1) = "a"
> arr(1,1) = "b"

> Thanks.

> Fred



Sat, 08 Feb 2003 10:06:43 GMT  
 Two dimensional array

// Dim arr(1,1)
var arr = new Array()
var anotherArr = new Array()

anotherArr[0] = 1        // arr(0,0) = 1
anotherArr[1] = "a"      // arr(0,1) = "a"
arr[0] = anotherArr

anotherArr = new Array()
anotherArr[0] = 2        // arr(1,0) = 2
anotherArr[1] = "b"       // arr(1,1) = "b"
arr[1] = anotherArr

// to access:

alert(arr[0][1])  // displays"a"
alert(arr[1][0])  // displays 2


Quote:
> Hi,

> This seems to be a very stupide question, but I just can't create a two
> dimensional array in JScript like in VBScript.

> What should be those lines of codes in JScript :

> Dim arr(1,1)
> arr(0,0) = 1
> arr(1,0) = 2
> arr(0,1) = "a"
> arr(1,1) = "b"

> Thanks.

> Fred



Sat, 08 Feb 2003 13:50:33 GMT  
 Two dimensional array


Quote:


> > JScript doesn't support multi-dimensional arrays.  You can fake it with
an
> array of arrays...

> Which essentially is multi-dimensional arrays, you just declare them
> differently ;)

Actually, they are quite different...

A multi-dimensional array has fixed bounds in all but the last dimension,
and every sub-array (for want of a better phrase) must be of the same size.

With an array of arrays, (or array of arrays of arrays...) every individual
array can be any arbitrary length, you can assign new arrays to the existing
elements, etc.

And of course there is a huge implementation difference, but that's another
story ;-)

Peter

--
Peter J. Torr - Microsoft Windows Script Program Manager

Please do not e-mail me with questions - post them to this
newsgroup instead. Thankyou!



Sat, 08 Feb 2003 03:00:00 GMT  
 Two dimensional array
Thank you Peter, I knew you wouldn't just let that pass ;-)...

--
Michael Harris
Microsoft.MVP.Scripting
--



Sat, 08 Feb 2003 03:00:00 GMT  
 Two dimensional array

Quote:


> > Which essentially is multi-dimensional arrays, you just declare them
> > differently ;)

> Actually, they are quite different...

> A multi-dimensional array has fixed bounds in all but the last dimension,
> and every sub-array (for want of a better phrase) must be of the same size.

> With an array of arrays, (or array of arrays of arrays...) every individual
> array can be any arbitrary length, you can assign new arrays to the existing
> elements, etc.

  Heh, which comes back to idea WHY there is stoopid thing
  in Array constructor like it is defining the initial length.

  For ? sake, this JS is not like Pascal or C where you are facing
  the problems how big or small one has to declare the array type,
  so it will not become overflooded or just not to allocate too
  much valuable memory for an array ...

  So whats the point ? As far as I am concerned the JS arrays are
  internally  sort of nested lists with nodes each have
  ID (index). Am I right ?

   Declaring

   myarr = new Array(999)

   doesnt allocate 999 memory for 999 objects.
   It just does nothing. When it does smth, then it is creating
   a new pointer and evaluates it with NULL, that later points
   to some object node ... Am I right ?

  I dont get the BASIC ARRAY CONSTRUCTOR DESIGN PRINCIPE IDEAS.
  Why there is an oppurtunety to declare array initial length,
  if the array size is "automodifiable",  AND EVEN
  CANNOT BE LOCKED TO FIXED SIZE ...

  The story Netscape had it so ... Well Array is available since
  NN3, msie3 hasnt got any. Then Netscape released NN4, which
  started to support JS1.2. And they threw away that "one argument
  initial length declaring". Which design by Array constructor I like.
  Sadly with coming of the late Netscape4s they took the
  old "one argument initial length declaring"  back.
   And I have no clue why ...

  The story, that netscape supports it, so we did it with msie the
  same is not so correct. Why then the String functions dont work
  correctly in pre v5.5 msies if you want to be "compatible" with nn.

  Well I tried to philopsope in thread "how to create arrays ?"

MM anno MM  (Marek M?nd aus Estland)
http://my.tele2.ee/cadorsoft/cdr_js_pkb/links.htm



Tue, 11 Feb 2003 03:00:00 GMT  
 Two dimensional array


Quote:
>    Declaring

>    myarr = new Array(999)

>    doesnt allocate 999 memory for 999 objects.
>    It just does nothing. When it does smth, then it is creating
>    a new pointer and evaluates it with NULL, that later points
>    to some object node ... Am I right ?

Pretty much.

I've made many posts about arrays and objects in the past, and you can
probably find them on http://www.deja.com/usenet

When you assign something to a property of an Array object, it checks to see
if such a property exists; if not, it creates it, and if so, it over-writes
what was already there. Then it checks to see if the property name is a
number, and if so, checks if the 'length' needs to be updated.

Quote:
>   I dont get the BASIC ARRAY CONSTRUCTOR DESIGN PRINCIPE IDEAS.
>   Why there is an oppurtunety to declare array initial length,
>   if the array size is "automodifiable",  AND EVEN
>   CANNOT BE LOCKED TO FIXED SIZE ...

One potential reason is that you might want to do something like:

var myImages = new Array(document.images.length);

for (i = 0; i < myImages.length; i++)
{
    // initialise the array element

Quote:
}

Peter

--
Peter J. Torr - Microsoft Windows Script Program Manager

Please do not e-mail me with questions - post them to this
newsgroup instead. Thankyou!



Tue, 11 Feb 2003 03:00:00 GMT  
 Two dimensional array

Quote:
> One potential reason is that you might want to do something like:

> var myImages = new Array(document.images.length);

> for (i = 0; i < myImages.length; i++)
> {
>     // initialise the array element
> }

 That value can be EASILY STORED somewhere else.
 And as by normal programming languages array is not an object,
 so its the only reasonable way to save in some kind of
 integer data type the array length.

 So that feature You mentioned is not good enough to have dumped
 JS1.2 compatible Array constructor. Well if You waant just test
 it for fun just use this here

<script language="JavaScript">alert((new Array(5)).length)</script>
<script language="JavaScript1.1">alert((new Array(5)).length)</script>
<script language="JavaScript1.2">alert((new Array(5)).length)</script>
<script language="JavaScript1.3">alert((new Array(5)).length)</script>
<script language="JavaScript1.4">alert((new Array(5)).length)</script>
<script language="JavaScript1.5">alert((new Array(5)).length)</script>

 The sad thing is JS has no inbuilt method that allows to set
 all elements in variable (in this context array) to 0
 or other value ...

 I mean like  fillchar(myarray,0,sizeOf(myarray)) // fills myarray with 0s

 The array length is pretty unuseful when some elements are left
 undefined. In loops funny things then happen ...
 (e.c. undefined + 5 = ?)

MM anno MM  (Marek M?nd aus Estland)
http://my.tele2.ee/cadorsoft/cdr_js_pkb/links.htm



Wed, 12 Feb 2003 03:00:00 GMT  
 Two dimensional array

Quote:

>  The sad thing is JS has no inbuilt method that allows to set
>  all elements in variable (in this context array) to 0
>  or other value ...

>  I mean like  fillchar(myarray,0,sizeOf(myarray)) // fills myarray with 0s

Why on earth would you feel the need to have such an easy and trivial task embedded in the language
core?

function fillchar(arr,content){
    for (var i=arr.length-1; i>0; i--)
        arr[i] = content;

Quote:
}
>  The array length is pretty unuseful when some elements are left
>  undefined. In loops funny things then happen ...
>  (e.c. undefined + 5 = ?)

Surely, no one blindly does mathematical operations on unknown or not-existing content. A simple if
statement will tell you wether the current position in the array has contents.

--
Thor Larholm



Wed, 12 Feb 2003 03:00:00 GMT  
 Two dimensional array

Quote:
> function fillchar(arr,content){
>     for (var i=arr.length-1; i>0; i--)
>         arr[i] = content;
> }

uh,

    i >= 0

:-)

Or the more "conventional"

    i = 0; i < arr.length; i++

Peter

--
Peter J. Torr - Microsoft Windows Script Program Manager

Please do not e-mail me with questions - post them to this
newsgroup instead. Thankyou!



Wed, 12 Feb 2003 03:00:00 GMT  
 Two dimensional array


Quote:


> > function fillchar(arr,content){
> >     for (var i=arr.length-1; i>0; i--)
> >         arr[i] = content;
> > }

> uh,

>     i >= 0

> :-)

Whoopzy, a little typo from my side ;)

Quote:
> Or the more "conventional"

>     i = 0; i < arr.length; i++

Actually, the "conventional" is slower, as you are reading the length property over and over again,
instead of just once.

Basically, one could do this in three ways.

1:
for (var i = 0; i < arr.length; i++)
2:
var l = arr.length;
for (var i = 0; i < l; i++)
3:
for (var i = arr.length-1; i>=0; i--)

The first one is the conventional way, and without doubt the easiest to learn, but also the slowest,
as the length property is read over and over again.
The second one is faster, as the length property is only read once. However, an additional variable
is defined and must be discarded later, either manually or by garbage collection.
The third one is the most optimized, as no additional variable is defined and the length property is
still read only once. However, the recursion order is reversed, going from arr.length to 0 instead
of from 0 to arr.length.

--
Thor Larholm



Wed, 12 Feb 2003 03:00:00 GMT  
 Two dimensional array
It's somewhat amusing that in your "trivial" example you made a
counting error. (We all do this, of course, which is one reason we
build things into languages -- so we don't have to copy or rewrite
"trivial" code over and over and sometimes make mistakes. You
were looking for a reason. This is one.)

Quote:
> Actually, the "conventional" is slower, as you are reading the

length property over and over again,

Quote:
> instead of just once.

True, but we don't always write code to be optimized on speed.
Sometimes it's better to optimize on speed of writing code,
readability, likelihood of being correct, etc.

Quote:
> Basically, one could do this in three ways.

Hmmm. Is that all? :)

Quote:
> The third one is the most optimized, as no additional variable is

defined and the length property is

Quote:
> still read only once.

OK, but I don't know if anyone asked for that.

Quote:
> However, the recursion order is reversed, going from arr.length
to 0 instead
> of from 0 to arr.length.

I think you mean the iteration order or something. I don't see any
recursion here.

Sent via Deja.com http://www.deja.com/
Before you buy.



Thu, 13 Feb 2003 13:34:42 GMT  
 Two dimensional array

Quote:


> >  I mean like  fillchar(myarray,0,sizeOf(myarray)) // fills myarray with 0s
> Why on earth would you feel the need to have such an easy and trivial task embedded in the language
> core?

 But why not ? I think inbuilt methods run far more faster than
 JScript "workarounding" functions.

 What the scripting lacks is also IMHO sets and their operations
 like in Pascal (SET OF)... Well the dictionary object seems to be sort
 of filling these tasks, but its an activeX object and not a
 part of language CORE ... that means it runs only on very few
 computers, cause it doesnt run on Netscape and others, and by
 MSIE many people have turned off ActiveX capabilities in security ...

Quote:
> >  The array length is pretty unuseful when some elements are left
> >  undefined. In loops funny things then happen ...
> >  (e.c. undefined + 5 = ?)

> Surely, no one blindly does mathematical operations on unknown or not-existing content. A simple if
> statement will tell you wether the current position in the array has contents.

 yes... I admit my talk i becoming senseless ...

 Ok, I have asked it before here, but didnt get any anwer.
 What is that unstandardized "unknown" data type by InternetExplorer ?
 For example

 alert(typeof location.reload)
 // WHY ISNT THAT FUNCTION like by Netscape ?
 // also I have heard that on Mac some event handlers dont evaluate
 // to undefined, but to unknown, when they are not defined.
 // Heh and the most funny part is that Opera3 evaluates
 // typeof NONEXISTANTVAR   to null ...
 // this is F complicated, one just could not write simply
 // if(a== undefined)  ...

<script language="JavaScript1.2"> var sum=0
 // works perfectly in Netscape, throws error in MSIE
 a = new Array(5) // one integer argument is treated in nnav as a[0]=5
 for(var i=0; i < a.length; i++ ) sum += a[i]
 alert(sum)
</script>

<script language="JavaScript"> var sum=0
 // throws a runntime error in Netscape aswell as in MSIE
 a = new Array(5) // one integer argument is treated as a.length =5
 for(var i=0; i < a.length; i++ ) sum += a[i]
 alert(sum)
</script>

MM anno MM  (Marek M?nd aus Estland)
http://my.tele2.ee/cadorsoft/cdr_js_pkb/links.htm



Thu, 13 Feb 2003 03:00:00 GMT  
 Two dimensional array
so are those optimizing-statements true ?

Why not talk about more of them. For some months there were here
such also here. Well why not to publish such things
and somebody from MS tell us wheter they are myths or not  ...

--
MM anno MM  (Marek M?nd aus Estland)
http://my.tele2.ee/cadorsoft/cdr_js_pkb/links.htm



Thu, 13 Feb 2003 03:00:00 GMT  
 
 [ 21 post ]  Go to page: [1] [2]

 Relevant Pages 

1. I want two dimensional array length...

2. traditional two dimensional array

3. Two dimensional arrays and more

4. Two Dimensional Arrays

5. two dimensional array

6. VBScript Two Dimensional Array?

7. Displaying a two-dimensional array

8. two-dimensional array

9. Help with two dimensional dynamic arrays...

10. Array size of a 2 dimensional array

11. Referring to an object in a 2 dimensional array-not working

12. Multi-Dimensional Arrays

 

 
Powered by phpBB® Forum Software