Haha, silly me, I think you're right.  :)

Silly off-by-1 error, but the concept is still the same.  I think that is 
the most efficient way to do it. (TMEWTDI?)

Nick Reinking








crumley at belka.space.umn.edu, on 09/12/2000 02:50:50 PM
To: tclug-list at mn-linux.org @ PMDF
cc:  
Subject: Re: [TCLUG:21227] Newbie perl question

On Tue, Sep 12, 2000 at 02:13:28PM -0500, Nick.T.Reinking at 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 at fields.space.umn.edu |
Work: 612 624-6804 or -0378  |

---------------------------------------------------------------------
To unsubscribe, e-mail: tclug-list-unsubscribe at mn-linux.org
For additional commands, e-mail: tclug-list-help at mn-linux.org




-------------- next part --------------
---------------------------------------------------------------------
To unsubscribe, e-mail: tclug-list-unsubscribe at mn-linux.org
For additional commands, e-mail: tclug-list-help at mn-linux.org