A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: passing values from a for loop into another array

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3

    passing values from a for loop into another array

    I am retrieving values from a XML file and storing them into an array called wordList using Actionscript 3.

    Say these are the values of wordList bear, cat, dog, kite, chair

    I need to then format the values and store them in an another array called words so that the array called words looks like this

    words=[" bear", "cat", "dog", "kite", "chair"];

    How can I wrap each value of the array in quotation marks and pass them to the array called words?

    MY CODE AT THE MOMENT

    Code:
                               function RetrieveWords(puzzleInput:XML):void {
    
    
                var wordList:XMLList = puzzleInput.Word.puzWord;
    
    
                for (var i:int = 0; i < wordList.length(); i++)
                {
                    var wordElement:XML = wordList[i];
                     pWord.push(wordElement);
    
                    trace(pWord[i]);
    
                        }   
                        trace("words");
                        pWord.toString();
                        trace(pWord);
    
                }

    trace(pWord[i]) lists the words like this bear cat dog kite chair

    ***trace(pWord) lists the values as

    bear,cat,dog,kite,chair

    I need to wrap each value in quotes and pass it to the array called words so that it looks like this

    words=["bear", "cat", "dog", "kite", "chair"]

  2. #2
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    PHP Code:
    var wordList:Array=new Array("bear""cat""dog""kite""chair");
    var 
    words:Array=new Array();

    for (var 
    i=0i<wordList.lengthi++) {
        if (
    typeof(wordList[i])=="string") {
            
    words.push(wordList[i]);
        } else {
            
    trace(typeof(wordList[i]));

        }
    }
    trace(words); 
    Check this.....



    arkitx

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Quote Originally Posted by arkitx View Post
    PHP Code:
    var wordList:Array=new Array("bear""cat""dog""kite""chair");
    var 
    words:Array=new Array();

    for (var 
    i=0i<wordList.lengthi++) {
        if (
    typeof(wordList[i])=="string") {
            
    words.push(wordList[i]);
        } else {
            
    trace(typeof(wordList[i]));

        }
    }
    trace(words); 
    Check this.....



    arkitx
    Thanks for the reply. The values can't be hard coded as they are been read from an xml file. I have an array called words that I could easily hard code the words into, however it needs to be dynamic.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Location
    Philadelphia, PA
    Posts
    4
    Can I ask why the quotes need to be stored in the array? Try this in your code:

    pWord.push('"'+wordElement+'"');

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