Click to See Complete Forum and Search --> : String to double array. Help?


boy3696
04-06-2004, 11:52 AM
Greetings...
I managed to load the double dimension array with a string, but I'm not very happy with the coding. Perhaps someone can tell me how to apply this a better way?
Below is the code I'm using.

<SCRIPT LANGUAGE="JavaScript">
var myString = "0~1~2~3~4|1~5~6~7~8|2~9~0~1~2|3~3~4~5~6";

var myTempArray = myString.split("|");

//Write out the array values from the first split.
for (i=0; i < myTempArray.length; i++) {
document.write("<br>myTempArray[" + i + "] = " + myTempArray[i] );
}

//Create a new array which contains the double dimension array.
document.write("<br>");
var myNewArray = new Array();

for (j=0; j < myTempArray.length; j++) {
var myNewArray2 = myTempArray[j].split("~");
myNewArray[j] = new Array();
for (k=0; k < myNewArray2.length; k++) {
myNewArray[j][k] = myNewArray2[k];
}
}

for (a=0;a<myNewArray.length;a++){ //myNewArray is the array that we've already created earlier
for (b=0;b<myNewArray[a].length;b++){
document.write("<br>myNewArray[" + a + "][" + b + "] = " + myNewArray[a][b]);
}
}
</script>

steelersfan88
04-06-2004, 03:55 PM
Same effect, just shorter:<script type="text/javascript">

var myString = "0~1~2~3~4|1~5~6~7~8|2~9~0~1~2|3~3~4~5~6";
var myTempArray = myString.split('|')
var dispStr = new String

for(var i=0;i<myTempArray.length;i++) {
dispStr += "myTempArray["+ i +"] = "+ myTempArray[i] +"<BR>"
myTempArray[i] = myTempArray[i].split('~')
for(var j=0;j<myTempArray[i].length;j++) {
dispStr += "<li>myTempArray["+ i +"]["+ j +"] = "+ myTempArray[i][j] +"<BR>"
}
}

document.write(dispStr)

</script>

boy3696
04-06-2004, 04:53 PM
Thanks steelersfan88...
I appreciate that very much. Once you look at something over and over, your brain just can't function anymore.
I'm going to streamline my code like you suggested.
Cheers buddy. :cool:

steelersfan88
04-06-2004, 05:16 PM
sure, and as I forgot in the beginning, welcome to the forums :)