[tpm] how to delete a list entry from within a foreach

Uri Guttman uri at StemSystems.com
Wed May 13 09:50:16 PDT 2009


>>>>> "FH" == Fulko Hew <fulko.hew at gmail.com> writes:

  FH> I know this is probably simple, but I'm having a mental block,
  FH> and I can't find the 'easy' solution in any book right now...

  FH> If I'm foreach'ing over a 'pick list', I need to get a positive indication
  FH> that a match was found and then remove it from the 'pick list'.

you can't delete from a list in a for. you can delete from an array but
it could screw up the for loop. 

  FH> something like:

  FH> if requestedvalue is on the picklist {
  FH>    remove it from the list
  FH>    return found
  FH> } else {
  FH>    return notfound
  FH> }

sounds like grep to me. grep out the element. compare the old size to
the new size of the array and return that boolean.

but using an array for deletion is a red flag. that is best done with a
hash. unless you need ordering, a hash is the answer and then it becomes
a simple call to delete and return the boolean based on that.

uri

-- 
Uri Guttman  ------  uri at stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


More information about the toronto-pm mailing list