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

Re: arrays: delete()/exists() + possible keys()/values()



Mark Mielke writes:
: The ONLY thing "unexpected-like" is the fact that scalar(@a)/$#a are
: affected if $a[N] represents the last initialized value in an array.
: However, I don't see any code "breaking" as a result of this... The
: end-of-array indicator has always been much looser than most people
: ever realized. This is more of a wakeup call than anything. Especially
: to those drawing incorrect diagrams to the masses and spreading
: falsehoods in an effort to more easily express language concepts.

That's a valid pedagogical technique... :-)

: Personally, I sort of like Larry's suggested:
: 
:          scalar(keys(%hash))    vs    scalar(keys(@array))
: 
: With the added benefit that scalar(keys(@array)) would not return
: the last element in the array like people assumed $#array to do (wrongly).
: 
:     scalar(keys(@array))
:     =
:     do {
:         my $keys = 0;
:         my $i;
: 
:         for ($i = 0; $i < @array; $i++) {
:             $keys++ if exists $array[$i];
:         }
: 
:         $keys;
:     };

I'd optimize that to:

    if (array_is_known_to_be_sparse()) {
        my $keys = 0;
        my $i;

        for ($i = 0; $i < @array; $i++) {
            $keys++ if exists $array[$i];
        }

        $keys;
    }
    else {
	scalar @keys;
    }

Generally speaking the implementation knows when it's sparsifying an array,
since it has to fill in the appropriate non-existent values.

I imagine by the time Perl 6 rolls around, sparse arrays will be possible
to link in on the fly as if they were an internal implementation.  In fact,
I believe that was one of Chip's specific design goals.

Larry


Follow-Ups from:
Chip Salzenberg <chip@valinux.com>
References to:
"Mark Mielke" <markm@nortelnetworks.com>

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