On Tue, 2008-10-07 at 22:01 -0400, Jay Savage wrote:
> On Tue, Oct 7, 2008 at 4:09 PM, Rob Dixon <[EMAIL PROTECTED]> wrote:
> > John W. Krahn wrote:
> [snip]
> >> Incorrect, delete does not remove array elements:
> >>
> >> $ perl -le'use Data::Dumper; my @a = "a".."d"; delete $a[1]; print
> >> Dumper [EMAIL PROTECTED]'
> >> $VAR1 = [
> >> 'a',
> >> undef,
> >> 'c',
> >> 'd'
> >> ];
> >
> > According to exists() it does.
> >
> > Rob
> >
>
> exists() lies, or is lied to.
>
> 'delete $array[$x]' does not mimic the behavior of 'splice(@array, $x,
> 1)', except in the special case where $x == $#array (and, I suppose $x
> == -1)...and even that doesn't always do what you expect. In
> particular, delete can occasionally delete more than you think it
> should.
>
> See perldoc -f delete for details.
>
> The important information in this context, though, is that delete()
> causes further tests of exists() to fail, but in most cases doesn't
> actually remove the element, because that would mean renumbering the
> indicies of the other array elements, which is what splice is for.
exists() did not lie, Data::Dumper did.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;
$Data::Dumper::Maxdepth = 0;
my @a = 'a' .. 'd';
delete $a[1];
$a[2] = undef;
for my $i ( 0 .. $#a ){
if( exists $a[$i] ){
if( defined $a[$i] ){
print "\$a[$i] exists and is defined: $a[$i]\n";
}else{
print "\$a[$i] exists but is not defined\n";
}
}else{
print "\$a[$i] does not exists\n";
}
}
print 'Dumper : ', Dumper [EMAIL PROTECTED];
__END__
--
Just my 0.00000002 million dollars worth,
Shawn
Linux is obsolete.
-- Andrew Tanenbaum
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/