A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: Replace with random words in array

  1. #1
    Senior Member
    Join Date
    Jul 2010
    Posts
    111

    Replace with random words in array

    Basically, I want something where I click a button, and it replaces words with other synonyms automatically. My thoughts are:

    set up array

    (array1=array2)

    array1="word1","word2","word3"
    array2="word1","word2","word3"

    search text for word in array1

    if find in text= word in array1
    replace with random word in array2

    Would anyone know how to set this up, or point me in the right direction?

    Thanks!

  2. #2
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    Code:
    for each word in array1
        while currentword is still found in the text
            i=position of currentword in text
            newText=substring of text from 0 to i-1
            newText+=random word from array2
            newText+=substring from i+currentword.length to text.length
            text=newText
    That's the basic idea
    Z¡µµ¥ D££

    Soup In A Box

  3. #3
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    Thanks! Would you know what the actionscript would be? I know some, but still am kind of new to the coding. I need it to search a dynamic text field for the words in the array (1) and replace each instance of the words with a random word from the other array (2). Here is my try:

    on(press){
    Wordarr = new Array("apple","orange","cherry");
    MyWords = new Array("apple","orange","cherry");
    MyWord = MyWords[random(3)]
    myTextVar2 = myTextVar.split(Wordarr).join(MyWord);
    }

    It is copying the word 'apple' from text field 1 (var name 'myTextVar') into another text field (var name 'myTextVar2'), but isn't changing any of the words. Am I totally off?
    Last edited by sammy123123; 07-23-2010 at 11:31 PM.

  4. #4
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    Code:
    for(word in Wordarr){
       while(myTextVar.indexOf(word)>-1){
            var indx=myTextVar.indexOf(word);
            myTextVar2=myTextVar.substr(0,i)+MyWords[random(MyWords.length)]+myTextVar.substr(i+word.length,myTextVar.length);
        }
    }
    Last edited by ZippyDee; 07-24-2010 at 01:28 AM. Reason: kicks and giggles
    Z¡µµ¥ D££

    Soup In A Box

  5. #5
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    Thanks, but I couldn't get it to work..

    I got close, though:

    on(press){
    Wordarr = ["apple","orange","cherry"]
    MyWords = ["apple","orange","cherry"]
    r = Math.floor(Math.random()*3);

    myTextVar2 = myTextVar.split("apple").join(MyWords[r]);
    }

    Only problem is, in the last statement, the 'split' isn't letting me use my Wordarr, so I needed to put "apple". I want it to go through and replace every instance of the words in my Wordarr with a random word in MyWords. Sooo close, I can touch it!
    Last edited by sammy123123; 07-24-2010 at 11:42 PM.

  6. #6
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    Hmmm...
    Code:
    myTextVar2=myTextVar;
    for(i=0;i<Wordarr.length;i++){
    	var word=Wordarr[i];
       while(myTextVar2.indexOf(word)>-1){
            var indx=myTextVar2.indexOf(word);
            myTextVar2=myTextVar2.substr(0,indx)+MyWords[random(MyWords.length)]+myTextVar2.substr(indx+word.length,myTextVar2.length);
        }
    }
    Try that.
    Last edited by ZippyDee; 07-24-2010 at 11:53 PM. Reason: Kicks and giggles
    Z¡µµ¥ D££

    Soup In A Box

  7. #7
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    Where do I put this?

  8. #8
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    onPress(){
    //put it here
    }
    Z¡µµ¥ D££

    Soup In A Box

  9. #9
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    ok..

    I put in 1st frame:

    Wordarr = ["apple","orange","cherry"]
    MyWords = ["apple","orange","cherry"]

    and on button:

    on(press){myTextVar2=myTextVar;
    for(i=0;i<Wordarr.length;i++){
    var word=Wordarr[i];
    while(myTextVar2.indexOf(word)>-1){
    var indx=myTextVar2.indexOf(word);
    myTextVar2=myTextVar2.substr(0,indx)+MyWords[random(MyWords.length)]+myTextVar2.substr(indx+word.length,myTextVar2.len gth);
    }
    }
    }

    It is working, except the word "cherry" is never showing.. it is okay, though, I just put some unused word (like "ducks" at the end of each words list) and it works great. Thanks!!!!
    Last edited by sammy123123; 07-25-2010 at 12:20 AM.

  10. #10
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    Say, when I enter a word like "all", and it changes it to "every", it also changes every instance of "all" in other words, like "eventually" turns into "eventueveryy" (no surprise)

    Is there a way to limit what it finds, by spaces in the sentence or something?

  11. #11
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    put this line right after the line "var indx=myTextVar2.indexOf(word);"

    Code:
    if(!(myTextVar2.charCodeAt(indx-1)>=97 && myTextVar2.charCodeAt(indx-1)<=122) && !(myTextVar2.charCodeAt(indx+word.length)>=97 && myTextVar2.charCodeAt(indx+word.length)<=122))
    That should do it.
    Z¡µµ¥ D££

    Soup In A Box

  12. #12
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    Hi, thanks, I tried it, looks like it wants to work, but my dumb computer is not letting me run it. It says the script is making it run slow. Dont know why, im sure it works, though. Thanks again!!

  13. #13
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    oh, of course. How stupid of me.
    Code:
    myTextVar2=myTextVar;
    for(i=0;i<Wordarr.length;i++){
        var word=Wordarr[i];
        var indx=myTextVar2.indexOf(word);
            while(myTextVar2.indexOf(word,indx)>-1){
            indx=myTextVar2.indexOf(word,indx);
            if(!(myTextVar2.charCodeAt(indx-1)>=97 && myTextVar2.charCodeAt(indx-1)<=122) && !(myTextVar2.charCodeAt(indx+word.length)>=97 && myTextVar2.charCodeAt(indx+word.length)<=122)){
            myTextVar2=myTextVar2.substr(0,indx)+MyWords[random(MyWords.length)]+myTextVar2.substr(indx+word.length,myTextVar2.length);
            indx++;
        }
    }
    hopefully that should be fully working....
    Z¡µµ¥ D££

    Soup In A Box

  14. #14
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    Hi, I tried this, but then it says:

    block must be terminated by '}'
    on(press){myTextVar2=myTextVar;

    so I added a } at the end, but then it just slows and is unresponsive again..

    on(press){myTextVar2=myTextVar;
    for(i=0;i<Wordarr.length;i++){
    var word=Wordarr[i];
    var indx=myTextVar2.indexOf(word);
    while(myTextVar2.indexOf(word,indx)>-1){
    indx=myTextVar2.indexOf(word,indx);
    if(!(myTextVar2.charCodeAt(indx-1)>=97 && myTextVar2.charCodeAt(indx-1)<=122) && !(myTextVar2.charCodeAt(indx+word.length)>=97 && myTextVar2.charCodeAt(indx+word.length)<=122)){
    myTextVar2=myTextVar2.substr(0,indx)+MyWords[random(MyWords.length)]+myTextVar2.substr(indx+word.length,myTextVar2.len gth);
    indx++;
    }

    }
    }

  15. #15
    Senior Member
    Join Date
    Jul 2010
    Posts
    111
    Hi, just thought I'd say that I figured it out, and thanks again for your help.. I am truly grateful for your golden advice!!

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