Re: splicing and empty rows
- From: Dave O <cxreg pobox com>
- To: muppet <scott asofyet org>
- Cc: ArtÅras Ålajus <x11 h2o pieva net>, gtk-perl list <gtk-perl-list gnome org>
- Subject: Re: splicing and empty rows
- Date: Wed, 17 Dec 2003 12:33:12 -0600 (CST)
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]