A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: remove item from array

  1. #1
    Senior Member creekmonkey's Avatar
    Join Date
    Jul 2006
    Posts
    121

    remove item from array

    I have an array of 10 numbers;
    cnum=new Array(1,2,3,4,5,6,7,8,9,10);

    //edit box to show random number from array
    for(i=0;i<cnum.length;i++){
    my_edit=random(cnum[i])}

    if there a way to remove a number from the array once it has shown in the edit box?

  2. #2
    Member
    Join Date
    Jan 2006
    Posts
    34
    Yes! There's a method for the Array object; array.remove(Index position). This method RETURNS the value, it doesn't modify the array. Which means you'd have to set a variable, or in this case the cnum array. So I'd right something like:

    Code:
    for(i=0;i<cnum.length;i++)
    {
    i2 = random(cnum[i]);
    my_edit = i2;
    cnum = cnum.replace(i2);
    }
    Good luck, .

    EDIT: Although this wont make much sense because, I do believe, it would be removing each index on every iteration. So I don't know if you want the last random number in the loop to be the number to remove, if so then change:

    Code:
    for(i=0;i<cnum.length;i++)
    {
    i2 = random(cnum[i]);
    my_edit = i2;
    if ((i+1) = cnum.lenth)
    {
    cnum = cnum.replace(i2);
    }
    }
    Last edited by Flash_Light; 01-07-2007 at 01:57 AM.

  3. #3
    Senior Member
    Join Date
    Dec 2006
    Posts
    274
    Quote Originally Posted by creekmonkey
    I have an array of 10 numbers;
    cnum=new Array(1,2,3,4,5,6,7,8,9,10);

    //edit box to show random number from array
    for(i=0;i<cnum.length;i++){
    my_edit=random(cnum[i])}

    if there a way to remove a number from the array once it has shown in the edit box?
    You have little bug in your code,my_edit=random(cnum[i]) returns random number that is between 0 and cnum[i], not any actual number from cnum array.

    And yes, cnum.remove(index) really removes data from array and after that array is shorter naturally.


    Code:
    cnum=new Array(1,2,3,4,5,6,7,8,9,10);
    
    buf = new String;
    for(i=0;i<cnum.length;i++){
    	buf += cnum[i];
    }
    trace ("Length: " + cnum.length);
    trace ("data: " + buf);
    
    cnum.remove(2);
    
    buf = "";
    for(i=0;i<cnum.length;i++){
    	buf += cnum[i];
    }
    trace ("Length: " + cnum.length);
    trace ("data: " + buf);
    I would do it this way;

    Code:
    cnum=new Array(1,2,3,4,5,6,7,8,9,10);
    
    n=cnum.length;
    numbers_left = n;
    
    for(var i=0; i<n;i++){
      rand_index = random(numbers_left);
      my_edit = cnum.remove(rand_index);
      numbers_left--;
      trace(my_edit);
    }
    or ultimate short version

    Code:
    cnum=new Array(1,2,3,4,5,6,7,8,9,10);
    
    while(cnum.length>0){
      my_edit = cnum.remove(random(cnum.length));
      trace(my_edit);
    }
    ofcource you can't see much but last number in you editbox as code runs quite fast
    Last edited by Finjogi; 01-07-2007 at 05:44 AM.

  4. #4
    Senior Member creekmonkey's Avatar
    Join Date
    Jul 2006
    Posts
    121
    Maybe this is just over my head, but the examples do not work for me. The work in the trace window, but remain in the array.

    What I want is to define an array in my start script
    cnum=new Array(1,2,3,4,5);

    have a button with the action
    for(i=0;i<cnum.length;i++){
    my_edit=random(cnum[i])}

    after the button is clicked I need whatever number is in the edit box to be removed fom the array, so when the button is clicked again it can not produce the previous number again.

  5. #5
    Senior Member creekmonkey's Avatar
    Join Date
    Jul 2006
    Posts
    121
    This actually works in preview but not when exported

    while(cnum.length>0){
    my_edit = cnum.remove(random(cnum.length));
    trace(my_edit);
    }

  6. #6
    Senior Member
    Join Date
    Dec 2006
    Posts
    274
    Ok, it seems that in Flash player exported .swf remove from array is buggy.
    Try attached movie in preview and Flash player. In flash player remove is not removing from right position (watch what numbers were in array and what was there after remove)

    I'll send bug report to 3dfa..
    Attached Files Attached Files

  7. #7
    Senior Member
    Join Date
    Dec 2006
    Posts
    274
    Ok, I found the "bug" and workaround to it.

    original way to get the number: (3dfa javascript)
    my_edit = cnum.remove(random(cnum.length));

    and the bug is that above script compiles inside swf to this:
    Code:
    (Actionscript code, not 3dfa Javascript)
    
     cnum.splice(random(cnum.length), 1); 
     my_edit = cnum[random(cnum.length)];
    Nothing wrong IF you do not use functions that migh return different numbers at different calls
    so, use temp variable to store random number and use it as index: (3dfa javascript)

    r_num = random(cnum.length);
    my_edit = cnum.remove(r_num);


    that compiles to:
    Code:
    (Actionscript code, not 3dfa Javascript)
    
            r_num = random(cnum.length);
            cnum.splice(r_num, 1);
            my_edit = cnum[r_num];
    edit: Sorry for confusion, compiled code was just actionscript code inside swf's, not code you use in 3fda (which is ~javascript..)
    Last edited by Finjogi; 01-12-2007 at 02:25 AM.

  8. #8
    Senior Member creekmonkey's Avatar
    Join Date
    Jul 2006
    Posts
    121
    Thanks finjogi !!!!! That helps a lot and gives me something to build on.

  9. #9
    Senior Member sadako232's Avatar
    Join Date
    Mar 2006
    Posts
    153
    Very nice finjogi! Creekmonkey i can't wait too see what your gunna make out of this.

    -sada-

  10. #10
    Senior Member creekmonkey's Avatar
    Join Date
    Jul 2006
    Posts
    121
    anytime I try one of the examples which uses array.splice() i get this error

    splice() is not a method of Array on line 3 cnum.splice(r_num, 1)

    This does work
    r_num = random(cnum.length);
    my_edit = cnum.remove(r_num);

    but still allows numbers in the array to be repeated
    Last edited by creekmonkey; 01-11-2007 at 07:52 PM.

  11. #11
    Senior Member creekmonkey's Avatar
    Join Date
    Jul 2006
    Posts
    121
    the splice method does work in flashmx but not in 3dfa

    this code does work both in preview and export

    if(cnum.length>0){
    tmp = random(cnum.length);
    trace(tmp);
    edit_box = cnum.remove(tmp);
    }

  12. #12
    Senior Member
    Join Date
    Dec 2006
    Posts
    274
    creekmonkey: Sorry for confusion, compiled code was just code that 3dfa produces, not what you use in your movies.

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