Graal Forums  

Go Back   Graal Forums > Development Forums > Tech Support
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-24-2006, 12:42 PM
JustBreathe JustBreathe is offline
Registered User
Join Date: Jun 2006
Posts: 59
JustBreathe is on a distinguished road
foreach missing element!

PHP Code:
function onCreated()
  {
  
temp.var = {1,2,3};
  for ( 
temp.vtemp.var )
    {
    echo( 
temp.);
    if ( 
temp.== )
      
temp.var.removetemp.);
    }
  } 
Output:
PHP Code:
1

That is... When you remove an index within a for ( element: array ), the following element is skipped... That is, the index of the array isn't backed up by the amount of indexes deleted/removed.
Reply With Quote
  #2  
Old 06-24-2006, 01:46 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
You are not supposed to modify the array of a for-each loop, it
would be too hard to detect insertion or removal of array members
and still run the for-each loop for each member. We should add
that to the wiki-docu though.

btw: firefox crashed two times when typing this reply, wondering why x-x
Reply With Quote
  #3  
Old 06-24-2006, 02:44 PM
JustBreathe JustBreathe is offline
Registered User
Join Date: Jun 2006
Posts: 59
JustBreathe is on a distinguished road
Custom Array's would solve a lot of these problems...

PHP Code:
for ( element: array )

maps to:

while ( 
element = array.getNextIndex() )

// Array Class
publc function deleteindex )
  {
  if ( 
index this.curr_index )
    
this.curr_index --;

  for ( 
0this.size(); i++)
    if ( 
index )
      
this.element.(@ ) = this.element.(@ 1);
  }

public function 
size()
  {
  while ( 
this.element.(@temp.count) != false temp.count ++;
  return 
temp.count;
  }

public function 
getNextIndex()
  {
  
this.curr_index ++;
  if ( 
this.size() < this.curr_index )
    return 
false;
  return 
thisthis.curr_index ];
  } 
That would fix it... But that would require something like new TGraalArray();

NOTE: This would also have to remap array[ index ] to array.element.(@ index )
Reply With Quote
  #4  
Old 06-24-2006, 03:17 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
It's not always that simple, e.g. it must be 'thread-safe', when you call sleep() inside the loop then it must still be working once the timeout occurs. That's why it just remembers the array variable and the index, most of the time that should be ok. If you need more control then you can use a normal for-loop I guess.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:05 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.