Re: splicing and empty rows



On Sat, 13 Dec 2003, muppet wrote:

you actually need to do this:

   sub del_user {
       my $bywhat = shift;
       for (0   {$UserList->{'data'}}-1) {
           if ($UserList->{data}[$_][1] eq $bywhat) {
               splice @{$UserList->{'data'}}, $_, 1;
               last; # stop!
           }
       }
       update_busers();
   }

in general, if you need to modify the list while traversing it, and may
be removing elements, either traverse it from back to front, or start
over after every modification.

Another safe way to do this is to back up every time you splice one out,
ie:

    sub del_user {
        my $bywhat = shift;

        my $n = 0;
        while($n <= $#{$UserList->{'data'}}) {
            if ($UserList->{data}[$_][1] eq $bywhat) {
                splice @{$UserList->{'data'}}, $_, 1;
                $n--;
            }
            $n++;
        }
        update_busers();
    }

It allows you to make a full iteration each time through no matter how
many are removed and wont skip or auto-vivify anything :-)  HTH

        Dave



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]