PDA

View Full Version : Multi-Dimensional Arrays



susie123
09-11-2007, 12:04 PM
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

boogyman
09-11-2007, 01:15 PM
1. don't forget the doctype. 4.01 strict


<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 ?

susie123
09-11-2007, 02:20 PM
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

boogyman
09-11-2007, 02:31 PM
try


<!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

susie123
09-11-2007, 02:49 PM
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!!!!

boogyman
09-11-2007, 03:27 PM
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


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


<!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>

susie123
09-11-2007, 03:49 PM
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 :)

boogyman
09-11-2007, 03:57 PM
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.



<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

jscheuer1
09-11-2007, 04:01 PM
I tried:





<!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:


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

susie123
09-11-2007, 04:39 PM
Great. Thank you both Boogyman and John :D