note
aufrank
<p>the problem is that there are still 10 elements if you just <code>undef</code> the match(es):</p>
<code>my @array = 1..10;
print "@array\n";
my $item = 3;
foreach my $element (@array) {
undef $element if ($element eq $item);
}
print "@array\n";
print $#array + 1;
</code>
<p>see, the array doesn't actually shrink. when you do a <code>print $#array + 1</code> you still get a 10 (assuming there were 10 elements to start like there were here).</p>
<p>to get rid of the phantom element(s), you could use [splice] like this:</p>
<code>my @array = 1..10;
print "@array\n";
my $item = 3;
my @empties;
my $count;
foreach my $element (@array) {
if ($element eq $item) {
undef $element;
push @empties, $count;
}
$count ++;
}
foreach (@empties) {
splice (@array, $_, 1);
}
print "@array\n";
print $#array + 1;
</code>
<p>hth,<br />
--au</p>
188669
188669