Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to go through an array with a loop, test each element, and eliminate it from said array if it meets a certain condition. I'm using splice with a C-style for loop. However, the array always ends up empty (the array has 2 elements and should only be throwing out one). P.S. - the array is an array of array references.
my $i; for ($i = 0; $i < @un; $i++) { my $username = $un[$i]; my @username = @$username; $sth->execute($username[0]) or die $dbh->errstr; $uid = $sth->fetchrow_array; unless ($uid) { warn "User $username[0] missing.\n"; # remove element from array splice (@un, $i, 1); next; } $info{$uid} = $role; }
Is this because you can't splice an array while iterating over it in a loop? Other problem? Any thoughts appreciated.
Thanks,
AEB
|
|---|