Shoutbox

[split] multi-dimensional arrays - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [split] multi-dimensional arrays (/showthread.php?tid=70544)

[split] multi-dimensional arrays by Spunky on 01-09-2007 at 04:00 PM

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
RE: [Question] Timer usage by L. Coyote on 01-09-2007 at 04:13 PM

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... :P
RE: [Question] Timer usage by Spunky on 01-09-2007 at 04:28 PM

quote:
Originally posted by L. Coyote
Btw, from what I gather, it's not the timer, but what you do with it, what matters

:rofl: 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)

RE: [Question] Timer usage by Matti on 01-09-2007 at 05:10 PM

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. :P

Another way you could do it, would be:
code:
var arr = new Array(new Array("thing", "stuff"), new Array("crap", "blah"));

RE: [Question] Timer usage by tryxter on 01-09-2007 at 06:47 PM

[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]
RE: [Question] Timer usage by CookieRevised on 01-10-2007 at 07:38 AM

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. :P

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.
RE: [Question] Timer usage by tryxter on 01-10-2007 at 11:25 AM

[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]


RE: [Question] Timer usage by CookieRevised on 01-10-2007 at 07:24 PM

[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]
RE: [Question] Timer usage by tryxter on 01-10-2007 at 09:47 PM

[OFF TOPIC]

Well, you're right.
Maybe I haven't searched correctly.

Thanks

[/OFF TOPIC]