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

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



On Thu, Jan 13, 2000 at 04:08:15PM -0700, "Tom Christiansen" wrote:
> >Ever considered that your little diagrams before were _WRONG_? There are
> >no undef()'s in @array[3..49]... they just act like undef() when they are
> >needed.
> I reject that view.  If I cannot distinguish between these conditions,
> they are not different.  And if I can distinguish, you've introduced
> painfully subtle complexity that we don't need.
> --tom

Think:

         %a = ('ABC' => undef);

         print "ABC defined\n"   if defined($a{'ABC'});
         print "DEF defined\n"   if defined($a{'DEF'});

         @a = (undef);

         print "[0] defined\n"   if defined($a[0]);
         print "[1] defined\n"   if defined($a[1]);

Now exists():

         %a = ('ABC' => undef);

         print "ABC exists\n"    if exists($a{'ABC'});
         print "DEF exists\n"    if exists($a{'DEF'});

         @a = (undef);

         print "[0] exists\n"    if exists($a[0]);
         print "[1] exists\n"    if exists($a[1]);

And then with delete()?      

         %a = ('ABC' => undef);  delete $a{'ABC'};

         print "ABC exists\n"    if exists($a{'ABC'});
         print "DEF exists\n"    if exists($a{'DEF'});

         @a = (undef);           delete $a[0];

         print "[0] exists\n"    if exists($a[0]);
         print "[1] exists\n"    if exists($a[1]);

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.

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;
    };

So:

    my @a;
    $a[100] = 1;
    print "$#a\n";                    # Prints 100
    print scalar(@a), "\n";           # Prints 101
    print scalar(keys(@a)), "\n";     # Prints "1"

And here's the kicker:

    my @a;
    $a[5] = 2;
    $a[7] = 3;
    print join(" ", keys(@a)), "\n";  # Prints "5 7"
    print join(" ", values(@a)), "\n";# Prints "2 3"

Tell me that isn't beautiful... :-) Well I know some people will... but
others will appreciate the complete elegance...

Let's see... what were the other operators? each()?

    my @a;
    $a[5] = 2;
    $a[7] = 3;
    while (($k, $v) = each @a) {
        print "\$a[$k] = $v;\n";
    }

Would Print:

    $a[5] = 2;
    $a[7] = 3;

Comone Tom... beauty? art? Haha...

Your happy camper,
mark

-- 
markm@nortelnetworks.com/mark@mielke.cc/markm@ncf.ca __________________________
.  .  _  ._  . .   .__    .  . ._. .__ .   . . .__  | Neighbourhood Coder
|\/| |_| |_| |/    |_     |\/|  |  |_  |   |/  |_   |
|  | | | | \ | \   |__ .  |  | .|. |__ |__ | \ |__  | Ottawa, Ontario, Canada

  One ring to rule them all, one ring to find them, one ring to bring them all
                       and in the darkness bind them...

                           http://mark.mielke.cc/


Follow-Ups from:
Larry Wall <larry@wall.org>
Chip Turner <chip@zfx.com>
References to:
Tom Christiansen <tchrist@chthon.perl.com>

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