splicing and empty rows



as a followup... you asked earlier about why SimpleList was leaving empty rows when you deleted a row, and we got sidetracked by the fact that splice wasn't fully supported.

at some point, i think in IRC, you mentioned that now that you're using the fixed SimpleList from CVS, you're still seeing empty rows.

after looking at your code from CVS, i think i found out why --- it's a pitfall for Perl list operations, so i thought i'd copy the list, i hope you don't mind.


you had:

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

the problem here is that you *always* iterate over the whole list. say you have two items in the list -- you hit index zero, delete it, and then proceed to index 1, despite the fact that the list only has one element and index 1 is no longer valid. so, perl autovivifies the index and the tie magic winds up leaving you with an empty row.

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.

--
muppet <scott at asofyet dot org>




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