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

Doug Lalonde douglalonde at gmail.com
Wed May 13 09:53:22 PDT 2009


Maybe something along these lines:

my @list=("1", "2", "3");
my $target = "2";
my $found=false;
my $cleanedList;
foreach $item(@list){
    if($target eq $item){
        $found = true;
    }else{
        push (@cleanedList,$item );
    }
}
@list = @cleanedList;

print @list;

2009/5/13 Fulko Hew <fulko.hew at gmail.com>

>
> I know this is probably simple, but I'm having a mental block,
> and I can't find the 'easy' solution in any book right now...
>
> If I'm foreach'ing over a 'pick list', I need to get a positive indication
> that a match was found and then remove it from the 'pick list'.
>
> something like:
>
> if requestedvalue is on the picklist {
>    remove it from the list
>    return found
> } else {
>    return notfound
> }
>
> Being Perl, I'm sure there is an elegant way... If only I knew.
>
>
> _______________________________________________
> 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/20090513/81223aa3/attachment.html>


More information about the toronto-pm mailing list