-
splitting one variable into pieces
Is there a method to split one variable into different variables?
Ex: I have a text input field with variable "text" when I type for example "this ;should ;be ;split" I want the 4 words to be assigned to variables 1, 2, 3 and 4...
so that the ; is the point where it splits, can be any carracter, best would be the space.
It realy needs to come from ONE variable since this SWF will be controlled by an external app that can only take one variable...
Thanks a lot
Last edited by Mr3s; 07-31-2003 at 01:07 PM.
-
you could do somethings like ...
best thing to do would be to use the string.split() .. function .. as mentioned in the following post:
http://www.flashkit.com/board/showth...hreadid=477484
or try this more manual approach:
wordArray = new Array();
var text = "split this text pls";
var splitHere = 0;
for (var i=0; i<text.length; i++){
if (text.charAt(i) == " "){
var temp = text.substring(splitHere, i); // chop string
wordArray.push(temp); // add cut word to array
splitHere = (i+1); // plus 1 to move past space
}
if (i == (text.length-1)){
// for last word
var temp = text.substring(splitHere, text.length);
wordArray.push(temp);
}
}
trace(wordArray);
then you could make another for loop to load the array values into vars ... or you could just simply load them straight into vars in the first place .. depends on whether you know what the user is typing .. which is hard .. sorry is a bit raggedy code .. but works as is
Last edited by jrdata2k; 07-31-2003 at 01:31 PM.
-=-
-
OK this works fine! thats what i need... BUT i'm not that familiar with arrays so can somebody explain how to connect the elements in the array to variables?
Thanks a lot!
-
you reference arrays .. like so ...
array[0] // where '0', is a numbered index that beigns at zero, basically a collection of variables...
yours uses 4 vars .. so you can reference each variable like
array[0]
array[1]
array[2]
array[3]
but the beatuy is you could just do this
// these are your text vars
var textVar_1, textVar_2, textVar_3, textVar_4
for (var i=0; i<array.length; i++){
this["textVar_"+i] = array[i];
}
-
-
sorry just noticed .. code ... stuffed the last bit:
// these are your text vars
var textVar_1, textVar_2, textVar_3, textVar_4
for (var i=0; i<array.length; i++){
this["textVar_"+(i+1)] = array[i];
}
needed to add in the very last line .. i+1, cause the array index starts at zero, but your variable name start at 1 .. sorry !!
-
and the array was named wordarray.... a friend helped me with thatone works fine now! thanks a lot...
// these are your text vars
var text0, text1, text2, text3;
for (var i = 0; i<wordArray.length; i++) {
this["text"+(i+1) ] = wordArray[i];
}
thanks a lot!
-
funny, i overlooked the first post about using the string.split() .. function... much easier. And since it will be myself who needs to type in the lines, it wont give problems
thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|