Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: 2d array

  1. #1
    New Coder
    Join Date
    Dec 2007
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    2d array

    I am trying to get a 2d array sorted but it doesn't seem to work.
    ANy ideas?
    It only ever prints 5 rows and each row is identical:


    Code:
    <script language = "javascript" type="text/javascript">
    var structure = new Array();
    structure[0] = new Array(5);
    
    structure[0,0] = 3;
    structure[0,1] = "SY400";
    structure[0,2] = "SY410";
    structure[0,3] = "SY414";
    structure[0,4] = "ZACY";
    structure[1,0] = 5;
    structure[1,1] = "SY600";
    structure[1,2] = "SY610";
    structure[1,3] = "SY610";
    structure[1,4] = "ZALY";
    structure[2,0] = 3;
    structure[2,1] = "SY400";
    structure[2,2] = "SY410";
    structure[2,3] = "SY410";
    structure[2,4] = "ZAVI";
    structure[3,0] = 4;
    structure[3,1] = "SY300";
    structure[3,2] = "SY340";
    structure[3,3] = "SY344";
    structure[3,4] = "ZBBC";
    structure[4,0] = 1;
    structure[4,1] = "SY700";
    structure[4,2] = "SY700";
    structure[4,3] = "SY700";
    structure[4,4] = "ZBDX";
    structure[5,0] = 4;
    structure[5,1] = "SY300";
    structure[5,2] = "SY340";
    structure[5,3] = "SY345";
    structure[5,4] = "ZBFP";
    
    document.write ("<table>");
    document.write(structure.length);
    for (i=0;i<structure.length;i++)
    {
    document.write ("<tr>");
    document.write("<td>" + i + "</td>");
    document.write("<td>" + structure [i,0] + "</td>");
    document.write("<td>" + structure [i,1] + "</td>");
    document.write("<td>" + structure [i,2] + "</td>");
    document.write("<td>" + structure [i,3] + "</td>");
    document.write("<td>" + structure [i,4] + "</td>");
    document.write ("</tr>");
    }
    document.write ("<table>");
    </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Your syntax is incorrect. Should be:-

    structure[0][1] = "SY400"; // and so on


    For a good tutorial see:-

    http://www.trans4mind.com/personal_d...pt/Array2D.htm

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Also:
    Code:
    var structure = new Array(6);
    for(var i = 0; i < structure.length; ++i)
      structure[i] = new Array(5);
    Trinithis


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •