Results 1 to 9 of 9

Thread: multi-dimensional array mystery - javascript (solved but may be of interest)

  1. #1

    Thread Starter
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404

    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.

  2. #2
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    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.

  3. #3
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    Hes right,

    Code:
    arrayname[[x],[y]]='text';
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

  4. #4

    Thread Starter
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    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.

  5. #5
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    yeah, that is a bit odd. I wonder why it does it
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

  6. #6
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    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

  7. #7
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    *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.

  8. #8

    Thread Starter
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    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.....!

  9. #9
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    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
  •  



Click Here to Expand Forum to Full Width