TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [TCLUG:21227] Newbie perl question
On Tue, Sep 12, 2000 at 02:13:28PM -0500, Nick.T.Reinking@supervalu.com wrote:
> for (my $i = 0; $i <= $#addrlist; $i++)
> {
> if ($addrlist[$i] eq $addr) { splice (@addrlist, $i, 1); }
> }
Doesn't this break the array subscripts though. You had an array
of 4 items (0=a 1=b 2=c 3=d), and you pull out the first one (index 0).
Now you have (0=b 1=c 3=d). You for statement happily goes on to check
the next index 1, but it has skipped over checking b because the
indices change.
Anyway, I could have sworn I had that problem, so I would run the
loop in reverse.
for (my $i = $#addrlist; $i >=0; $i--) {
if ($addrlist[$i] eq $addr) { splice (@addrlist, $i, 1); }
}
This way you don't run into problems with indices changing
underneath you.
--
Jim Crumley |
crumley@fields.space.umn.edu |
Work: 612 624-6804 or -0378 |