...

View Full Version : 2d array



qwertyjjj
12-20-2007, 05:51 PM
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:




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

Philip M
12-20-2007, 07:03 PM
Your syntax is incorrect. Should be:-

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


For a good tutorial see:-

http://www.trans4mind.com/personal_development/JavaScript/Array2D.htm

Trinithis
12-20-2007, 11:03 PM
Also:


var structure = new Array(6);
for(var i = 0; i < structure.length; ++i)
structure[i] = new Array(5);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum