A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: splitting one variable into pieces

  1. #1
    Junior Member
    Join Date
    Dec 2002
    Posts
    20

    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.

  2. #2
    Junior Member
    Join Date
    Jun 2002
    Posts
    24
    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.
    -=-

  3. #3
    Junior Member
    Join Date
    Dec 2002
    Posts
    20
    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!

  4. #4
    Junior Member
    Join Date
    Jun 2002
    Posts
    24
    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];
    }
    -=-

  5. #5
    Junior Member
    Join Date
    Dec 2002
    Posts
    20
    OK thanks!

  6. #6
    Junior Member
    Join Date
    Jun 2002
    Posts
    24
    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 !!
    -=-

  7. #7
    Junior Member
    Join Date
    Dec 2002
    Posts
    20
    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!

  8. #8
    Junior Member
    Join Date
    Dec 2002
    Posts
    20
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center