Results 1 to 10 of 10

Thread: Multi-Dimensional Arrays

  1. #1
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Multi-Dimensional Arrays

    Please can anybody tell me why/whatīs wrong with this code that it wonīt show in IE browser?

    <html>
    <head>
    <title> Multi-Dimensional Arrays</title>
    </head>

    <body>
    <script language="text/JavaScript">
    var personnel = new Array();

    personnel[0] = new Array();
    personnel[0] [0] = "Nathaly";
    personnel[0] [1] = "8";
    personnel[0] [2] = "Cuenca";

    personnel[1] = new Array();
    personnel[1] [0] = "David;
    personnel[1] [1] = "31";
    personnel[1] [2] = "Essex";

    personnel[2] = new Array();
    personnel[2] [0] = "Marlon";
    personnel[2] [1] = "38";
    personnel[2] [2] = "Barcelona";

    document.write("Name : " + personnel [1] [0] + personnel [2] [0] + personnel [3] [0] + "<br>");
    document.write("Age : " + personnel [1] [1] + personnel [2] [1] + personnel [3] [1] + "<br>");
    document.write("Address : " + personnel[1] [2] + personnel[2] [2] + personnel[3] [2]);

    </script>
    </body>
    </html>

    Many thanks

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    1. don't forget the doctype. 4.01 strict
    Code:
    <DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
           <meta http-equiv="content-type" content="text/html; charset=utf8">
    ...
    2.
    ersonnel[1] = new Array();
    personnel[1] [0] = "David;
    personnel[1] [1] = "31";
    personnel[1] [2] = "Essex";
    you forgot the ending double quote on david

    3.
    document.write("Name : " + personnel [1] [0] + personnel [2] [0] + personnel [3] [0] + "<br>");
    document.write("Age : " + personnel [1] [1] + personnel [2] [1] + personnel [3] [1] + "<br>");
    document.write("Address : " + personnel[1] [2] + personnel[2] [2] + personnel[3] [2]);
    there is no personnel[3] did you mean 0,1,2 ?
    Last edited by boogyman; 09-11-2007 at 01:22 PM.

  3. #3
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Multi-dimensional Arrays - Javascript

    Hey Mr Boogyman

    Thank you for your rapid response - it really is appreciated. I have reworked the code and it now looks like this... but still doesnīt show onscreen:

    <DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf8">

    <title> Multi-Dimensional Arrays</title>
    </head>

    <body>
    <script language="text/JavaScript">
    var personnel = new Array();

    personnel[0] = new Array();
    personnel[0] [0] = "Nathaly";
    personnel[0] [1] = "8";
    personnel[0] [2] = "Cuenca";

    personnel[1] = new Array();
    personnel[1] [0] = "David";
    personnel[1] [1] = "31";
    personnel[1] [2] = "Essex";

    personnel[2] = new Array();
    personnel[2] [0] = "Marlon";
    personnel[2] [1] = "38";
    personnel[2] [2] = "Barcelona";

    document.write("Name : " + personnel [0] [0] + personnel [1] [0] + personnel [2] [0] + "<br>");
    document.write("Age : " + personnel [0] [1] + personnel [1] [1] + personnel [2] [1] + "<br>");
    document.write("Address : " + personnel[0] [2] + personnel[1] [2] + personnel[2] [2]);

    </script>
    </body>
    </html>

    Any more ideas? Thank you. S

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    try
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title> Multi-Dimensional Arrays</title>
    </head>
    
    <body>
    <script type="text/javascript">
    var personnel = new Array();
    
    personnel[0] = new Array();
    personnel[0] [0] = "Nathaly";
    personnel[0] [1] = "8";
    personnel[0] [2] = "Cuenca";
    
    personnel[1] = new Array();
    personnel[1] [0] = "David";
    personnel[1] [1] = "31";
    personnel[1] [2] = "Essex";
    
    personnel[2] = new Array();
    personnel[2] [0] = "Marlon";
    personnel[2] [1] = "38";
    personnel[2] [2] = "Barcelona";
    
    document.write("Name : " + personnel[1][0] + personnel [2] [0] + personnel [0] [0] + "<br>");
    document.write("Age : " + personnel[1] [1] + personnel [2] [1] + personnel [0] [1] + "<br>");
    document.write("Address : " + personnel[1] [2] + personnel[2] [2] + personnel[0] [2]);
    
    </script>
    </body>
    </html>
    works for me in testing

  5. #5
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Multi-dimensional Arrays - Javascript

    Thank you again Boogyman. Two questions for you:

    1) How come I canīt show "personnel [0]" as the first column? Why does it have to come last?

    2) How can I make equal space between the names/ages/addresses ie align them?



    You really are becoming my best friend!!!!

  6. #6
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by susie123 View Post
    Thank you again Boogyman. Two questions for you:

    1) How come I canīt show "personnel [0]" as the first column? Why does it have to come last?
    because you have declared it last
    Quote Originally Posted by susie123 View Post
    2) How can I make equal space between the names/ages/addresses ie align them?
    now you are getting into the realm of changing the way you create the array. the way you have changed / called the array, it can be very easy to confuse what is what, like you did originally with the personnel[3]. if you create a multi-dimensional array and then loop thru using better key's you would probably have an easier time accomplishing everything
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title> Multi-Dimensional Arrays</title>
    
    
    <style type="text/css">
    div#personnel {margin: 0 auto; width: 50%;}
    div#personnel p {width: 100%; margin: .5em}
    div#personnel p span{width:33%}
    </style>
    
    <script type="text/javascript">
    var personnel = array(
          "0" => array(
                  "Name:" => "Nathalay", 
                  "Age:" => "8",
                  "Location" => "Cuenca",
          ),
          "1" => array(
                  "Name:" => "David", 
                  "Age:" => "31",
                  "Location" => "Essex",
          ),
          "2" => array(
                  "Name:" => "Marlon", 
                  "Age:" => "38",
                  "Location" => "Barcelona",
          ),
    etcetc
    );
    </script>
    
    </head>
    <body>
    
    
    <script type="text/javascript">
    document.write('<div id="personnel">');
    document.write("<p>Name: 
    <span>" +personnel[0]['name']+ "</span> 
    <span>" +personnel[1]['name']+ "</span> 
    <span>" +personnel[2]['name']+ "</span>
    </p>");
    document.write("<p>Age: 
    <span>" +personnel[0]['age']+ "</span> 
    <span>" +personnel[1]['age']+ "</span> 
    <span>" +personnel[2]['age']+ "</span>
    </p>");
    document.write("<p>Location: 
    <span>" +personnel[0]['location']+ "</span> 
    <span>" +personnel[1]['location']+ "</span> 
    <span>" +personnel[2]['location']+ "</span>
    </p>");
    document.write("</div>");
    </script>

  7. #7
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Thumbs up

    Boogyman...

    1) Of course!!
    2) OH! Donīt think I should go there just yet. Iīm following a course and havenīt covered those things yet. Donīt want to get ahead of myself. Think Iīll just have to remain happy that theyīre finally appearing onscreen for the timebeing rather than complicating matters.

    Thank you again

  8. #8
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by susie123
    2) OH! Don&#180;t think I should go there just yet. I&#180;m following a course and haven&#180;t covered those things yet.
    okay you can still do the output part though.

    Quote Originally Posted by myself
    <style type="text/css">
    div#personnel {margin: 0 auto; width: 50&#37;;}
    div#personnel p {width: 100%; margin: .5em}
    div#personnel p span{width:33%}
    </style>
    <script type="text/javascript">
    document.write('<div id="personnel">');
    document.write("<p>Name:
    <span>" +personnel[0]['name']+ "</span>
    <span>" +personnel[1]['name']+ "</span>
    <span>" +personnel[2]['name']+ "</span>
    </p>");
    document.write("<p>Age:
    <span>" +personnel[0]['age']+ "</span>
    <span>" +personnel[1]['age']+ "</span>
    <span>" +personnel[2]['age']+ "</span>
    </p>");
    document.write("<p>Location:
    <span>" +personnel[0]['location']+ "</span>
    <span>" +personnel[1]['location']+ "</span>
    <span>" +personnel[2]['location']+ "</span>
    </p>");
    document.write("</div>");
    </script>
    just change the name/age/location values to the 0,1,2 like you had it originally

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I tried:

    Quote Originally Posted by boogyman View Post
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title> Multi-Dimensional Arrays</title>
    
    
    <style type="text/css">
    div#personnel {margin: 0 auto; width: 50%;}
    div#personnel p {width: 100%; margin: .5em}
    div#personnel p span{width:33%}
    </style>
    
    <script type="text/javascript">
    var personnel = array(
          "0" => array(
                  "Name:" => "Nathalay", 
                  "Age:" => "8",
                  "Location" => "Cuenca",
          ),
          "1" => array(
                  "Name:" => "David", 
                  "Age:" => "31",
                  "Location" => "Essex",
          ),
          "2" => array(
                  "Name:" => "Marlon", 
                  "Age:" => "38",
                  "Location" => "Barcelona",
          ),
    etcetc
    );
    </script>
    
    </head>
    <body>
    
    
    <script type="text/javascript">
    document.write('<div id="personnel">');
    document.write("<p>Name: 
    <span>" +personnel[0]['name']+ "</span> 
    <span>" +personnel[1]['name']+ "</span> 
    <span>" +personnel[2]['name']+ "</span>
    </p>");
    document.write("<p>Age: 
    <span>" +personnel[0]['age']+ "</span> 
    <span>" +personnel[1]['age']+ "</span> 
    <span>" +personnel[2]['age']+ "</span>
    </p>");
    document.write("<p>Location: 
    <span>" +personnel[0]['location']+ "</span> 
    <span>" +personnel[1]['location']+ "</span> 
    <span>" +personnel[2]['location']+ "</span>
    </p>");
    document.write("</div>");
    </script>
    It didn't work, something about illegal assignments. This is much more in keeping with the original, and works:

    Code:
    <html>
    <head>
    <title> Multi-Dimensional Arrays</title>
    
    <style type="text/css">
    .info td {
    padding:.5ex;
    }
    </style>
    </head>
    
    <body>
    <table class="info">
    <script type="text/javascript">
    var r='<tr>', d='<td>', dc='</td>', rc='</tr>';
    var personnel = new Array();
    
    personnel[0] = new Array();
    personnel[0][0] = "Nathaly";
    personnel[0][1] = "8";
    personnel[0][2] = "Cuenca";
    
    personnel[1] = new Array();
    personnel[1][0] = "David";
    personnel[1][1] = "31";
    personnel[1][2] = "Essex";
    
    personnel[2] = new Array();
    personnel[2][0] = "Marlon";
    personnel[2][1] = "38";
    personnel[2][2] = "Barcelona";
    
    document.write(r+d+"Name : "+dc+d + personnel [0][0]+dc+d + personnel [1][0]+dc+d + personnel [2][0]+dc+rc);
    document.write(r+d+"Age : "+dc+d + personnel [0][1]+dc+d + personnel [1][1]+dc+d + personnel [2][1]+dc+rc);
    document.write(r+d+"Address : "+dc+d + personnel[0][2]+dc+d + personnel[1][2]+dc+d + personnel[2][2]+dc+rc);
    </script>
    </table>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Thumbs up

    Great. Thank you both Boogyman and John

Bookmarks

Posting Permissions

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