[tpm] how to delete array entries en-passant

Indy Singh indy at indigostar.com
Wed Jun 30 08:49:10 PDT 2010


Modifying the array while iterating through the array is usually something to be avoided when writing clear code.  Don't do it.

What would you expect the foreach function to return when you are modifying the list under its feet?

You may also want to look at using the grep and map functions.

Indy Singh
IndigoSTAR Software -- www.indigostar.com

  ----- Original Message ----- 
  From: Fulko Hew 
  To: TPM 
  Sent: Wednesday, June 30, 2010 11:06 AM
  Subject: [tpm] how to delete array entries en-passant


  As the subject line asks...
  what I'd like to do is something like

  foreach (@array) {
      if (condition) {
          splice @array, this_entry, 1;
          next;
      }
      do processing on this entry;    
  }

  @array now contains a sub-set of the original
  that I can now further process




  'delete' might work, but the words say delete on arrays is deprecated
  and it still doesn't give me the delete 'this' entry concept, because
  it too works on an index.

  I'd like to do it without creating another array, because I like
  'premature optimizations' and because I think it would make the
  code more readable.  :-)

  TIA
  Fulko




------------------------------------------------------------------------------


  _______________________________________________
  toronto-pm mailing list
  toronto-pm at pm.org
  http://mail.pm.org/mailman/listinfo/toronto-pm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20100630/104621da/attachment.html>


More information about the toronto-pm mailing list