|
-
May 20th, 2002, 05:39 AM
#1
Thread Starter
Hyperactive Member
multi-dimensional array mystery - javascript (solved but may be of interest)
Try the below script, it works as it should adding 'hello' to each
element of the array in turn.
Until it reaches element [2][0].
then it throws an 'object expected' error..*sighs deeply* am i
missing something blatantly obvious here can someone please
enlighten me?
Code:
<html>
<head>
</head>
<script>
var board_pos = new Array(new Array(), new Array());
for(a = 0; a <= 5; a++)
{
for(b = 0; b <= 6; b++)
{
alert (a + ' , ' + b);
board_pos[a][b] = 'hello';
}
}
</script>
<body>
</body>
</html>
Last edited by progressive; May 20th, 2002 at 06:59 AM.
-
May 20th, 2002, 06:05 AM
#2
Frenzied Member
It's been quite a while since I did arrays, so this might not be right, but the error goes if you do it like:
Code:
board_pos[[a][b]] = 'hello';
I think it may be something to do with the way you are declaring the array.
-
May 20th, 2002, 06:22 AM
#3
Fanatic Member
Hes right,
Code:
arrayname[[x],[y]]='text';
-
May 20th, 2002, 06:56 AM
#4
Thread Starter
Hyperactive Member
thanks guys !
Up 'till now I've never needed to use multi-dimensional arrays in
javascript and accoring to a book on the subject, Javascript
doesn't support true multi dimensional arrays.
This being the case I picked up the above method from a website somewhere.
After looking more closely the extra []'s makes sense.
Strange how it accepts values until it reaches elements [2][0] and greater though !
Ta.
-
May 20th, 2002, 07:40 AM
#5
Fanatic Member
yeah, that is a bit odd. I wonder why it does it
-
May 20th, 2002, 09:08 AM
#6
Frenzied Member
I think it's something to do with the fact that you're not initialising the arrays, this seems to works fine:
Code:
var board_pos = new Array(
[, , , , , ],
[, , , , , ],
[, , , , , ],
[, , , , , ],
[, , , , , ],
[, , , , , ],
[, , , , , ]);
for(a = 0; a <= 5; a++)
{
for(b = 0; b <= 6; b++)
{
alert (a + ' , ' + b);
board_pos[a][b] = 'hello';
}
}
I don't really know if you should do that or not, I'm not that great with JS
-
May 21st, 2002, 11:14 AM
#7
Frenzied Member
*shudder*
When you think of multideminsional arrays, you are thinking of matrices. So when you declare myArray(4, 4), you are declaring a square matrix, that is four elements on each side, with 16 elements total (4 x 4).
In JavaScript (and many other languages), you create an array of arrays. So if you declared myArray(Array(4), Array(4)), you are declaring an array with lenght 2, each element being an array with length 4. So you have 8 elements total (4 + 4).
If you wanted a 4x4 square matrix, you need to declare it as myArray(Array(4), Array(4), Array(4), Array(4)).
It was returning an error because board_pos[2] didn't exist, much less was board_pos[2] an array (implying that board_pos[2][0] didn't exist either).
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
May 21st, 2002, 11:34 AM
#8
Thread Starter
Hyperactive Member
gotcha !
cheers CiberTHuG 
Just to clarify if i wanted a 10 by 10 matix
i'd declare a new array:-
my_array = new Array(new Array(10),...repeated 9 times
and to access row 5 column 5 i'd use
my_array[4][4]
cool...that explains the weird results.....!
-
May 21st, 2002, 11:54 AM
#9
Frenzied Member
Exactly.
If I were to rewrite you code, I'd do the following.
Code:
<html>
<head>
</head>
<body>
<script>
var board_pos = new Array(6);
for(var a = 0; a < board_pos.length; a++){
board_pos[a] = new Array(7);
for(var b = 0; b < board_pos[a].length; b++){
alert (a + ' , ' + b);
board_pos[a][b] = 'hello';
}
}
</script>
</body>
</html>
Couple of important changes. First, nothing exists outside the head and body blocks. So you must but the script block in one or the other.
I use .length instead of some arbitrary number in the for loops. And I declare a and b.
The most important change, declare the second deminsion in the for loop. It is so much easier if the array is 10x10.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|