dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Named array numbers

  1. #1
    Member Manic_Man's Avatar
    Join Date
    Dec 2007
    Location
    Hastings, England
    Posts
    91

    Named array numbers

    okay.. I know the easy way to avoid this but

    If you have an array which has subarrays which have a string as the index (so for example MyArray["Apple"].price, MyArray["Bananna"].price etc) can the entries still be accessed as numbers? like if "Apple" was the first index in the array, then MyArray[0] would be the same as MyArray["Apple"]. I guess it would because surely it's flash that is converting it to allow a string as an index, so there is a number there.. but not sure.

  2. #2
    Member Alloy Bacon's Avatar
    Join Date
    Apr 2011
    Posts
    1,019
    To find the index for apple you could do, trace(myArray.indexOf("Apple")); and you would get its array position is that what you need? if the index is not found you will get the value -1 so if myArray.indexOf("Pear")==-1 you would see there is no pear and you can myArray.push("Pear");
    Last edited by Alloy Bacon; 12-15-2016 at 10:28 AM.
    you want to make a hypocrite out of everythinggg zomg

  3. #3
    Member Manic_Man's Avatar
    Join Date
    Dec 2007
    Location
    Hastings, England
    Posts
    91
    mmm.. forgot you could do that.. so that does seam to say that with names, they still have numbers.. so myArray[0] (if the index returns the same) is the same as myArray["Apple"]...

    thanks

  4. #4
    Member Alloy Bacon's Avatar
    Join Date
    Apr 2011
    Posts
    1,019
    No problem, if you enjoyed that script, feel free to buy me a doughnut, paypal: giancarlo.quarta@yahoo.com
    Last edited by Alloy Bacon; 12-15-2016 at 10:54 AM.
    you want to make a hypocrite out of everythinggg zomg

  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,784
    Hi,
    The way you have your array looks more like an associative array (object array) hence the .price on each, I don't think they have a length attribute but you can loop though the props of associative arrays to get a count.

    PHP Code:
    var cnt:Number 0;
    for(var 
    id:String in myArray)
    {
        
    cnt++;
        var 
    val:Object myArray[id];
        
    trace(cnt " / " id " = " val);


  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,784
    Hi,

    Unless of course you are using multi dimensional arrays

    PHP Code:
    var myArray1:Array = new Array("Apple","Banana");

    trace(myArray1["Apple"]);
    trace(myArray1[0]);

    trace(myArray1.indexOf("Apple"));

    trace("---------------------------");

    var 
    myArray2:Array = new Array(["Apple"],["Banana"]);

    trace(myArray2["Apple"]);
    trace(myArray2[0]);

    trace(myArray2.indexOf("Apple")); 
    notice the difference

  7. #7
    Member Manic_Man's Avatar
    Join Date
    Dec 2007
    Location
    Hastings, England
    Posts
    91
    yeah, it's and associative array but it wasn't more about the total length but the index of each element. thanks though

  8. #8
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,784
    Hi,

    Then i believe you need to reference them each by name.

  9. #9
    Member Alloy Bacon's Avatar
    Join Date
    Apr 2011
    Posts
    1,019
    http://www.TESP.co.uk looking nice Manic did you make the site, has a few graphic glitches when minimized abit though
    you want to make a hypocrite out of everythinggg zomg

  10. #10
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,784
    Hi,

    whatever happened to your site Alloy, it's good to have showcase of your own as well as having the google play area, I think people take you a little bit more serious if you have at least a home page.

  11. #11
    Member Alloy Bacon's Avatar
    Join Date
    Apr 2011
    Posts
    1,019
    its good its running on IIS 7.5 it wont be online for a while
    you want to make a hypocrite out of everythinggg zomg

  12. #12
    Member
    Join Date
    Oct 2016
    Posts
    54
    yes, nice Tesp. sega creature good.
    sorry not help with coded

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