Jay Savage wrote:
> On Tue, Oct 7, 2008 at 4:09 PM, Rob Dixon <[EMAIL PROTECTED]> wrote:
>> John W. Krahn wrote:
>>>
>>> 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.
> 
> 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.

The return value of exists() is the definition of whether an array or hash
elements exists. It does exactly what it is documented to do and does not lie.

The idea of non-existent mid-range array elements is unusual, although there is
at least Lua that does a similar thing.

splice() cannot make array elements non-existent except when the length of the
array is changed and tail elements vanish. To expect delete() to behave the same
way as splice() is to be dissatisfied with the Perl language definition: they
are different operations.

Rob


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to