Jay Savage wrote:
On Wed, Oct 8, 2008 at 11:24 AM, Rob Dixon <[EMAIL PROTECTED]> wrote:
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
No argument. The definitions of exists is "tests positive for
exists()," and a tautology is a thing which is tautological. In any
case, I'm not dissatisfied with the behavior. As I said, this is why
we have both delete and splice, which both have uses.
But the issue I was responding to isn't whether the element exists, it
was your rebuff of John's assertion that delete does not always remove
the item, which is correct.
Actually I was wrong, delete does remove an array element:
$ perl -le'use Devel::Peek; my $x = ["a".."d"]; delete @{$x}[1,2]; print
Dump $x'
SV = RV(0x8180028) at 0x8153660
REFCNT = 1
FLAGS = (PADBUSY,PADMY,ROK)
RV = 0x8152c28
SV = PVAV(0x8157dc4) at 0x8152c28
REFCNT = 1
FLAGS = ()
IV = 0
NV = 0
ARRAY = 0x816ebb8
FILL = 3
MAX = 3
ARYLEN = 0x0
FLAGS = (REAL)
Elt No. 0
SV = PV(0x8153b00) at 0x8152d78
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x816e8c8 "a"\0
CUR = 1
LEN = 4
Elt No. 1
Elt No. 2
Elt No. 3
SV = PV(0x8153c08) at 0x81536b4
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x8174b20 "d"\0
CUR = 1
LEN = 4
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/