David Inglis wrote:
>
> Is there a command to drop an element from an array, or what is the best
> way to do this.
Hi David.
A call to 'splice' is the usual answer, but sometimes you
may want the remaining elements to keep their indices.
A call to 'delete' will shrink an array if you delete from
its end. A delete from the middle of its extent will
leave the size the same but the 'exists' test will then
return false on that element.
Check out this code.
HTH,
Rob
use strict;
use warnings;
my @a = 'a' .. 'j';
printf "Original length: %d\n\n", scalar @a;
undef $a[2]; # undefine the third element
delete $a[4]; # delete the fifth element
delete $a[9]; # delete the last element
printf "New length: %d\n\n", scalar @a;
foreach my $i (0 .. 9) {
my $value;
if (not exists $a[$i]) {
$value = 'non-existent'
}
elsif (not defined $a[$i]) {
$value = 'undefined';
}
else {
$value = $a[$i];
}
printf "\$a[%d] = %s\n", $i, $value;
}
**OUTPUT
Original length: 10
New length: 9
$a[0] = a
$a[1] = b
$a[2] = undefined
$a[3] = d
$a[4] = non-existent
$a[5] = f
$a[6] = g
$a[7] = h
$a[8] = i
$a[9] = non-existent
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>