A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: AS3 dynamic array names???

  1. #1
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929

    AS3 dynamic array names???

    I've been trying to wrap my head around this for the past few days and have tried numerous iterations and thought someone might have some insight...

    I've got an array (slides) that I parsed from an amfphp return that contains the following data in each array element:
    slides = (chapterName, lessonName, slideName, slideText), for every slide in the course

    I'm trying to get arrays out this that will look like:
    chp1Array = list of Lesson names in that Chapter
    chp1Less1Array = list of slide names in that Lesson
    chp1Less2Array
    chp2Array
    chp2Less1Array
    chp2Less2Array
    chp2Less3Array
    etc. etc.

    This wouldn't be a problem in as2 but I can't get it to work in AS3 because it gives me errors when I try to create the arrays with dynamic names from within the function?

    I've tried several different methods, but none seem to quite get what I need because I can't figure out how to get the dynamic number into the array name...

    Any ideas??
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  2. #2
    Member
    Join Date
    Oct 2009
    Location
    Ontario
    Posts
    98
    Arrays changed a bit from AS2 to AS3.

    An associative array in AS3 looks like this:

    Code:
    var myArray:Object = {
         Name: "Bobby"
    };
    
    trace(myArray.Name); // outputs "Bobby"
    Furthermore, you can use variable names for the keys:

    Code:
    var myKey = "Name";
    var myArray:Object = {
         myKey: "Bobby"
    };
    
    trace(myArray.myKey); // also outputs "Bobby"
    And you can also nest arrays within arrays:

    Code:
    var myKey = "Info";
    var myArray:Object  = {
         myKey: {
              Name: "Bobby",
              Gender: "Male"
         }
    };
    
    trace(myArray.myKey.Gender); // outputs "Male"
    I'm not entirely sure what you're doing with your code, but this should help you in working with Arrays in AS3, I hope! =)
    Last edited by Nidht; 10-15-2009 at 05:35 PM.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    None of the stuff Nidht just posted is specific to Arrays. It's actually dynamic object literals. It works with an array because an array is a dynamic object, but if you're using it that way, just use an Object since you don't need anything an Array gives you.

    Also this is not using a variable for a key:
    Code:
    var myKey = "Name";
    var myArray:Object = {
         myKey: "Bobby"
    };
    
    trace(myArray.myKey); // also outputs "Bobby"
    In the above, the myKey property of myArray has NOTHING to do with the myKey variable declared above it. That said, it's good to know these things about Objects.

    For your actual question, you need to explain more. I don't understand what you have and what you want. But, you probably do NOT need to dynamically create variables.

    Could you put up some sample input and output?

  4. #4
    Member
    Join Date
    Oct 2009
    Location
    Ontario
    Posts
    98
    5TonsOfFlax, how does the myKey property of myArray have NOTHING to do with the myKey variable declared above it?

    I'm using it to output the value, and in a situation where a variable is passed to a function to return a value from an Array, isn't that using it as a key?

    I was just showing flashpipe1 that one could use variables to access keys in the Array.

  5. #5
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Nidht, 5Tons is right. A better example for what you're trying to do would be:
    Code:
    var myKey = "Name";
    var myArray:Object = {
         myKey: "Bobby"
    };
    
    trace(myArray.Name); // also outputs "Bobby"
    Which, won't work. You're trying to set the key to the value of a variable. For an example that DOES work, you'd need to do something like:
    Code:
    myKey = "Name";
    var myArray:Object = {
         Name: "Bobby"
    };
    
    trace(myArray[myKey]);

  6. #6
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929
    Thanks for the clarification on keys...I think...not sure how it differs from pushing to the array...but I'll look at that later...basically, I have a menu that I built to load dynamically (thanks for the help on that weeks ago 5tons) based on having chapter, lesson and slide titles broken up as follows:

    chp1Array = list of Lesson names in that Chapter
    chp1Less1Array = list of slide names in that Lesson
    chp1Less2Array
    chp2Array
    chp2Less1Array
    chp2Less2Array
    chp2Less3Array
    etc. etc.

    however, when I get my data from amfphp, I parsed that out to store the properties for each slide, so that array "slides" looks like:
    slides = (chapterName, lessonName, slideName, slideText), for every slide in the course

    I'm trying to figure out how to create the group of arrays I need to build the menu from the array I have that controls the slides...

    I was trying something like this, but I can't figure out how to create the new arrays within the script or within the functions...right now it's just pushing to chpArray (which is fine), but I also need a chpXArray (with all the lesson titles for that chapter), I'm pushing to lessArray (which needs to be chpXLessXArray, which needs to have all the slide titles for that lesson)...not sure if this makes my problem any clearer??

    Code:
    var chpArray:Array=new Array();
    var lessArray:Array=new Array();
    var slideArray:Array=new Array();
    function populateArrays():void {
    	for (var i:uint=0; i<slides.length; i++) {
    		if (chpArray.indexOf(slides[i][0])==-1) {
    			addChapter(slides[i][0]);
    			addLesson(slides[i][1]);
    			addSlide(slides[i][2]);
    		} else {
    			if (lessArray.indexOf(slides[i][1])==-1) {
    				addLesson(slides[i][1]);
    				addSlide(slides[i][2]);
    			} else {
    				addSlide(slides[i][2]);
    			}
    		}
    	}
    }
    function addChapter(chpName:String):void {
    	chpArray.push(chpName);
    }
    function addLesson(lessName:String):void {
    	lessArray.push(lessName);
    }
    function addSlide(slideName:String):void {
    	slideArray.push(slideName);
    }
    populateArrays();
    To test it stand-alone, I used the following code to create a sample slides array to test against:
    Code:
    var slides:Array = new Array();
    slides[0] = ["chp1Name", "less1Name", "slideName2", "slideText"];
    slides[1] = ["chp1Name", "less2Name", "slideName", "slideText"];
    slides[2] = ["chp1Name", "less2Name", "slideName2", "slideText"];
    slides[3] = ["chp2Name", "less1Name", "slideName1", "slideText"];
    slides[4] = ["chp2Name", "less1Name", "slideName2", "slideText"];
    slides[5] = ["chp2Name", "less2Name", "slideName", "slideText"];
    slides[6] = ["chp2Name", "less2Name", "slideName2", "slideText"];

    Thanks!!
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  7. #7
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929
    In AS2, I'd just set a variable for chapNum and lessNum and do something like this in my addLesson function:

    Code:
    this["chp"+(chpNum)+"less"+lessNum+"Array"] = new Array();
    this["chp"+(chpNum)+"less"+lessNum+"Array"].push(slides[i][2]);
    but AS3 won't let me do that...
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  8. #8
    Member
    Join Date
    Oct 2009
    Location
    Ontario
    Posts
    98
    Quote Originally Posted by MyFriendIsATaco View Post
    Nidht, 5Tons is right. A better example for what you're trying to do would be:
    Code:
    var myKey = "Name";
    var myArray:Object = {
         myKey: "Bobby"
    };
    
    trace(myArray.Name); // also outputs "Bobby"
    Which, won't work. You're trying to set the key to the value of a variable. For an example that DOES work, you'd need to do something like:
    Code:
    myKey = "Name";
    var myArray:Object = {
         Name: "Bobby"
    };
    
    trace(myArray[myKey]);
    Ah, fair enough. I misrepresented what I was trying to show. I meant to encase that variable in brackets, but I confused myself when I saw that it worked when I tested the code, before posting it. It would indeed restrict the user from accessing that value from its actual key name.

    I've been in the office far too long, today. =)

  9. #9
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Quote Originally Posted by flashpipe1 View Post
    In AS2, I'd just set a variable for chapNum and lessNum and do something like this in my addLesson function:

    Code:
    this["chp"+(chpNum)+"less"+lessNum+"Array"] = new Array();
    this["chp"+(chpNum)+"less"+lessNum+"Array"].push(slides[i][2]);
    but AS3 won't let me do that...
    Albeit messy, but who says you can't do that? It's perfectly valid. I just ran this to prove my point:
    Code:
    var chpNum:int = 1;
    var lessNum:int = 5;
    var i:int = 0;
    var slides:Array = new Array();
    slides[i] = new Array();
    slides[i][2] = "Awesome!";
    this["chp"+(chpNum)+"less"+lessNum+"Array"] = new Array();
    this["chp"+(chpNum)+"less"+lessNum+"Array"].push(slides[i][2]);
    trace(this["chp"+(chpNum)+"less"+lessNum+"Array"]);

  10. #10
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929
    MyFriendIsATaco,

    You're right, that works just fine...I had an error in another line of the code and assumed it was because of the arrays I was trying to name...jeesh!

    I debugged it, found the REAL problem and the array sorting/building is working fine...thanks!

    You're right, definitely messy. I should have mapped out all the pieces to work from the same initial array, but the menu functionality hadn't been defined by the client until after I had gotten the "paging" through slides function working...

    The menu builds dynamically, from top to bottom, and I couldn't figure out how to do that based on my existing array...any suggestions on how this might be set up better based on the little information I've given? Just trying to learn how to do this more efficiently next time...part of the issue is the way the data is returned from amfphp (just one big query string), so, I suppose I should have built the php to run the queries so I would read in the arrays in the order/format I needed them...i.e. do the chapter query first, then loop through each chapter to get the lessons, then the slides, etc...yes?

    Oh well, at least it's working now...

    Thanks!
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

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