Forum Moderators: open

Message Too Old, No Replies

parse text to variables

wondering how to convert words into a series of variables

         

boxlunch

4:43 am on Feb 16, 2005 (gmt 0)



I am trying to convert a word into a series of variables. Example- the word 'website' would be converted into 7 variables-
letter1 = "w'
letter2 = "e"
letter3 = "b"
etc.

Thanks for any help. I always find these forums very helpful, I finally have a problem that I couldn't find my answer to already on here.

Ben

Bernard Marx

8:02 am on Feb 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'd usually create an array, instead of a series of (pretty much unconnected) variables.

Split the string, with an empty string as argument:

var word = "website";
var letters = word.split("");

You can create global variables from this, if you really want. To do this, you treat globals as properties of the window:

for(var k=0;k<letters.length;k++)
window["letter"+(k+1)] = letters[k];

alert(letter3)