[split] multi-dimensional arrays |
Author: |
Message: |
Spunky
Former Super Mod
Posts: 3658 Reputation: 61
36 / /
Joined: Aug 2006
|
O.P. [split] multi-dimensional arrays
quote: Originally posted by tryxter
Off topic:
How can I declare a multidimensional array? like array[x][y]?
I tried "var xyz = new array(,)" and "new array([],[])" but it seems that isn't working...
I've already tried to read some reference but it's kind of messy on that part.
code: var myArray = new Array();
myArray[0][0] = "A1"
myArray[0][1] = "A2"
myArray[1][0] = "B1"
myArray[1][1] = "B2"
IIRC, it should be like that
This post was edited on 01-10-2007 at 11:16 PM by WDZ.
<Eljay> "Problems encountered: shit blew up"
|
|
01-09-2007 04:00 PM |
|
|
L. Coyote
Senior Member
Captain Obvious
Posts: 981 Reputation: 49
39 / /
Joined: Aug 2004
Status: Away
|
RE: [Question] Timer usage
quote: Originally posted by SpunkyLoveMuff
code: var myArray = new Array();
myArray[0][0] = "A1"
myArray[0][1] = "A2"
myArray[1][0] = "B1"
myArray[1][1] = "B2"
IIRC, it should be like that
Maybe not.
I once needed this type of array and had to do this:
code: var arr = new Array();
arr[0] = new Array();
arr[0][0] = "blah";
Btw, from what I gather, it's not the timer, but what you do with it, what matters.
quote: Originally posted by SpunkyLoveMuff
I've used multi-dimensional arrays before, but never with that method so I'm not sure if that is correct (I could be wrong though)
Well, JScript needs the Array variables to be predefined. If you don't tell it that arr[0] is an array, it'll tell you arr[0][0] is not defined.
I remember coming from using them in PHP and getting all these errors in JavaScript... I sort of learnt it the hard way...
This post was edited on 01-09-2007 at 04:40 PM by L. Coyote.
Hack, hack, hack!
Finally became a Systems Analyst!
|
|
01-09-2007 04:13 PM |
|
|
Spunky
Former Super Mod
Posts: 3658 Reputation: 61
36 / /
Joined: Aug 2006
|
O.P. RE: [Question] Timer usage
quote: Originally posted by L. Coyote
Btw, from what I gather, it's not the timer, but what you do with it, what matters
That sounds familiar
quote: Originally posted by L. Coyote
I once needed this type of array and had to do this:
code: var arr = new Array();
arr[0] = new Array();
arr[0][0] = "blah";
I've used multi-dimensional arrays before, but never with that method so I'm not sure if that is correct (I could be wrong though)
<Eljay> "Problems encountered: shit blew up"
|
|
01-09-2007 04:28 PM |
|
|
Matti
Elite Member
Script Developer and Helper
Posts: 1646 Reputation: 39
32 / /
Joined: Apr 2004
|
RE: [Question] Timer usage
quote: Originally posted by L. Coyote
I once needed this type of array and had to do this:
code: var arr = new Array();
arr[0] = new Array();
arr[0][0] = "blah";
That's also the way I do it. I have no idea how JScript would react if you try to add an array element to an array element which is undefined at that moment... Maybe I should try it.
Another way you could do it, would be:
code: var arr = new Array(new Array("thing", "stuff"), new Array("crap", "blah"));
This post was edited on 01-09-2007 at 05:15 PM by Matti.
|
|
01-09-2007 05:10 PM |
|
|
tryxter
Junior Member
A. Maia Ferreira @ PTnet
Posts: 51 Reputation: 1
35 / / –
Joined: Jan 2007
|
RE: [Question] Timer usage
[OFF TOPIC]
code: var arr = new Array();
arr[0] = new Array();
arr[0][0] = "blah";
Before you suggested that, I tried it and it worked perfectly.
I think this should be posted on the "Tips" section. Multidimensional arrays aren't well explained on MS documentation.
What procedure do you think it's best? I only tested one.
Well, thanks a lot to all your help.
[/OFF TOPIC]
This post was edited on 01-10-2007 at 09:48 PM by tryxter.
|
|
01-09-2007 06:47 PM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: [Question] Timer usage
quote: Originally posted by Mattike
quote: Originally posted by L. Coyote
I once needed this type of array and had to do this:
code: var arr = new Array();
arr[0] = new Array();
arr[0][0] = "blah";
That's also the way I do it. I have no idea how JScript would react if you try to add an array element to an array element which is undefined at that moment... Maybe I should try it.
Another way you could do it, would be:
code: var arr = new Array(new Array("thing", "stuff"), new Array("crap", "blah"));
Both ways are exactly the same
quote: Originally posted by tryxter
Multidimensional arrays aren't well explained on MS documentation.
Because they don't exist in JScript.
JScript does not support multidimensional arrays (which is well explained in the help page on the Array object).
To simulate multidimensional arrays you need to define a new array as part of another array element.
Hence, arr[0][0] isn't a multidemensional array (which would be only one object), it is an array which consists of other arrays (which are 1 + number of elements objects), which is quite different.
This post was edited on 01-11-2007 at 06:22 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
01-10-2007 07:38 AM |
|
|
tryxter
Junior Member
A. Maia Ferreira @ PTnet
Posts: 51 Reputation: 1
35 / / –
Joined: Jan 2007
|
RE: [Question] Timer usage
[OFF TOPIC]
Yes, I know they don't exist. In JScript, arrays are treated like objects, and that's "the problem".
But if there's a way to do (or simply simulate) it, it should be explained, don't you think?
Unless there's a better way to do what multidimensional arrays do...
[/OFF TOPIC]
This post was edited on 01-10-2007 at 09:49 PM by tryxter.
|
|
01-10-2007 11:25 AM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: [Question] Timer usage
[OFF TOPIC]
quote: Originally posted by tryxter
But if there's a way to do (or simply simulate) it, it should be explained, don't you think?
Which is clearly explained in the JScript documentation: quote: Although JScript does not directly support multi-dimensional arrays, you can store any sort of data inside array elements — including other arrays. So you can get the behavior of multi-dimensional arrays by storing arrays within the elements of another array. For example, the following code builds a multiplication table for the numbers up to 5:code: // Change this number for a bigger table
var iMaxNum = 5;
// Loop counters
var i, j;
// New array. Make it iMaxNum + 1 because arrays start
// counting at zero, not 1.
var MultiplicationTable = new Array(iMaxNum + 1);
// Loop for each major number (each row in the table)
for (i = 1; i <= iMaxNum; i++)
{
// Create the columns in the table
MultiplicationTable[i] = new Array(iMaxNum + 1);
// Fill the row with the results of the multiplication
for (j = 1; j <= iMaxNum; j++)
{
MultiplicationTable[i][j] = i * j;
}
}
window.alert(MultiplicationTable[3][4]); // Displays 12
window.alert(MultiplicationTable[5][2]); // Displays 10
window.alert(MultiplicationTable[1][4]); // Displays 4
Also, when one puts 1 plus 1 (elements of arrays can contain anything, including objects, and arrays are objects) you come automatically to how to make/simulate multidimensional arrays. I'm not sure if every basic thing you can do with objects, etc needs to be explained in 'tips'. It's basic programming...
[/OFF TOPIC]
This post was edited on 01-10-2007 at 07:32 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
01-10-2007 07:24 PM |
|
|
tryxter
Junior Member
A. Maia Ferreira @ PTnet
Posts: 51 Reputation: 1
35 / / –
Joined: Jan 2007
|
RE: [Question] Timer usage
[OFF TOPIC]
Well, you're right.
Maybe I haven't searched correctly.
Thanks
[/OFF TOPIC]
This post was edited on 01-10-2007 at 09:50 PM by tryxter.
|
|
01-10-2007 09:47 PM |
|
|
|