[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

Re: exists $foo[7] and delete $foo[7]



>Tom Christiansen writes:
>: >> I like the potential, but the well- (and strictly-) defined linear
>: >> nature of arrays will lead people to believe things like:
>: >> 
>: >>        $a[100] = '0-99 do not yet exist';
>: >>        # later in their code...
>: >>        exists $a[23] or die "Why not? \$a[100] exists!";
>: 
>: >Funny, I am always wondering why perl suddenly claims there are 101
>: >elements when I'm fairly sure I've only set 1.
>: 
>: Funny, I don't recall Perl ever saying that.

>Sure it does, if you say scalar(@a).  But then scalar(%a) is also
>strange, so it fits.  You have to use keys(@a) to find that actual
>number of existing elements.

Ah, that's it, then.  I internally read $#a as returning "the highest
index number at which something has been assigned to that array".
I don't really think of it as "number of elements minus one".  When
scalar(@a) came around, I kept the $#a thinking, slightly a bit.
That has lead me never to be in the least surprised or confused
that scalar(@a) doesn't give an answer that has anything to do with
the whether intervening elements in the array have ever been subjected
to assignment.  

--tom


[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]