Jump to content
Sign in to follow this  
dyei nightmare

deleting item from array in the same iteration?

Recommended Posts

just wanna know how can be deleted an item from an array in the same iteration,  this doesnt work.

  int miarray[] = {1,2,3,4,5,6,8,9,10}; 
  foreach(int i;  miarray)
  {

   if(somecondition == 0){
   
   pop(miarray,i);
  }


  }
    

since  aparently you cant reference the array itself from inside...  how do you delete an index in the same iteration? 

 

Share this post


Link to post
Share on other sites

ah the only thing that works is  in for() and with removevalue...  think it is because foreach cant update the array lenght per iteration???  

this works:

 int myarray[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13};
 int myarrlen = len(myarray);
 for(int i=0; i<myarrlen;i++){
      if(i%2==0){                
      removevalue(myarray,i);
      }
                               
  }
                               

 

 but should be a way to delete an item inside the same foreach somehow,  but how? 

Edited by dyei nightmare

Share this post


Link to post
Share on other sites
 i[]@miarray = {33,16,21,88,90}; 
 i@length = len(@miarray);

 for(int h=0; h<@length; h++){
  i@remover = @miarray[h];
   removevalue(@miarray,@remover);


} 

 ahhhh   now i found the real question...  why this doesnt remove everything in array?  it just leaves 2 items, is not removing everything but..

 whhhhhhyyyy!!!    it doesn make senseeeeeeeeeee!!!!!!!!!!!!

Share this post


Link to post
Share on other sites

 aaaaaahhhh solveeed!!! :P 

 now i understand why,  when removevalue is executed,  the array updates inmediatelly,  and the next iteration,  jumps any elements.   but it is logic if i think about it...  ;)

Edited by dyei nightmare

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×