/    Sign up×
Community /Pin to ProfileBookmark

String to double array. Help?

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>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Apr 06.2004 — Same effect, just shorter:[code=php]<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>[/code]
Copy linkTweet thisAlerts:
@boy3696authorApr 06.2004 — 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. ?
Copy linkTweet thisAlerts:
@steelersfan88Apr 06.2004 — sure, and as I forgot in the beginning, welcome to the forums ?
×

Success!

Help @boy3696 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...