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

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



On Thu, 13 Jan 2000 02:18:32 EST, Mark-Jason Dominus wrote:
>I notice that although the effect of deleting the last element of an
>array is documented, you have not documented the behavior of delete on
>any other element.  You only said that it `deletes' the elemnt, which
>could mean any of several things.  In particular, you did not say
>whether or not it is the same as `undef' in these cases.  I think you
>should explicitly say whether it is the same as undef or splice or
>whatever.
>
>Simiarly, I think you should have a clear statement about what
>`exists' actually does for arrays.  Is  exists($a[$n]) equivalent to
>($n >= 0 ? $n <= $#a : -$n <= @a), or what?

I fixed the docs some.


Sarathy
gsar@ActiveState.com
-----------------------------------8<-----------------------------------
Change 4797 by gsar@auger on 2000/01/13 08:12:56

	clearer docs for change#4796; faster av_exists()

Affected files ...

... //depot/perl/av.c#44 edit
... //depot/perl/pod/perldelta.pod#128 edit
... //depot/perl/pod/perldiag.pod#111 edit
... //depot/perl/pod/perlfunc.pod#127 edit

Differences ...

==== //depot/perl/av.c#44 (text) ====
Index: perl/av.c
--- perl/av.c.~1~	Thu Jan 13 00:13:00 2000
+++ perl/av.c	Thu Jan 13 00:13:00 2000
@@ -663,8 +663,11 @@
 	    return SvTRUE(sv);
 	}
     }
-    if (av_fetch(av, key, 0))
+    if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef
+	&& AvARRAY(av)[key])
+    {
 	return TRUE;
+    }
     else
 	return FALSE;
 }

==== //depot/perl/pod/perldelta.pod#128 (text) ====
Index: perl/pod/perldelta.pod
--- perl/pod/perldelta.pod.~1~	Thu Jan 13 00:13:00 2000
+++ perl/pod/perldelta.pod	Thu Jan 13 00:13:00 2000
@@ -430,13 +430,15 @@
 The exists() and delete() builtins now work on simple arrays as well.
 The behavior is similar to that on hash elements.
 
-exists() can be used to check whether an array element exists without
-autovivifying it.  If the array is tied, the EXISTS() method in the
-corresponding tied package will be invoked.
+exists() can be used to check whether an array element has been
+initialized without autovivifying it.  If the array is tied, the
+EXISTS() method in the corresponding tied package will be invoked.
 
-delete() may now be used to remove an element from the array and return
-it.  If the element happens to be the one at the end, the size of the
-array also shrinks by one.  If the array is tied, the DELETE() method
+delete() may be used to remove an element from the array and return
+it.  The array element at that position returns to its unintialized
+state, so that testing for the same element with exists() will return
+false.  If the element happens to be the one at the end, the size of
+the array also shrinks by one.  If the array is tied, the DELETE() method
 in the corresponding tied package will be invoked.
 
 See L<perlfunc/exists> and L<perlfunc/delete> for examples.

==== //depot/perl/pod/perldiag.pod#111 (text) ====
Index: perl/pod/perldiag.pod
--- perl/pod/perldiag.pod.~1~	Thu Jan 13 00:13:00 2000
+++ perl/pod/perldiag.pod	Thu Jan 13 00:13:00 2000
@@ -140,23 +140,23 @@
 if you are certain that you're calling the function correctly, you may put
 an ampersand before the name to avoid the warning.  See L<perlsub>.
 
-=item %s argument is not a HASH element
+=item %s argument is not a HASH or ARRAY element
 
-(F) The argument to exists() must be a hash element, such as
+(F) The argument to exists() must be a hash or array element, such as:
 
     $foo{$bar}
-    $ref->[12]->{"susie"}
+    $ref->[12]->["susie"]
 
-=item %s argument is not a HASH element or slice
+=item %s argument is not a HASH or ARRAY element or slice
 
-(F) The argument to delete() must be either a hash element, such as
+(F) The argument to delete() must be either a hash or array element, such as:
 
     $foo{$bar}
-    $ref->[12]->{"susie"}
+    $ref->[12]->["susie"]
 
-or a hash slice, such as
+or a hash or array slice, such as:
 
-    @foo{$bar, $baz, $xyzzy}
+    @foo[$bar, $baz, $xyzzy]
     @{$ref->[12]}{"susie", "queue"}
 
 =item %s did not return a true value

==== //depot/perl/pod/perlfunc.pod#127 (text) ====
Index: perl/pod/perlfunc.pod
--- perl/pod/perlfunc.pod.~1~	Thu Jan 13 00:13:00 2000
+++ perl/pod/perlfunc.pod	Thu Jan 13 00:13:00 2000
@@ -935,6 +935,10 @@
 a hash tied to a DBM file deletes the entry from the DBM file.  Deleting
 from a C<tie>d hash or array may not necessarily return anything.
 
+Deleting an array element effectively returns that position of the array
+to its initial, uninitialized state.  Subsequently testing for the same
+element with exists() will return false.  See L</exists>.
+
 The following (inefficiently) deletes all the values of %HASH and @ARRAY:
 
     foreach $key (keys %HASH) {
@@ -970,7 +974,6 @@
     delete $ref->[$x][$y][$index];
     delete @{$ref->[$x][$y]}[$index1, $index2, @moreindices];
 
-
 =item die LIST
 
 Outside an C<eval>, prints the value of LIST to C<STDERR> and
@@ -1404,9 +1407,9 @@
 =item exists EXPR
 
 Given an expression that specifies a hash element or array element,
-returns true if the specified element exists in the hash or array,
-even if the corresponding value is undefined.  The element is not
-autovivified if it doesn't exist.
+returns true if the specified element in the hash or array has ever
+been initialized, even if the corresponding value is undefined.  The
+element is not autovivified if it doesn't exist.
 
     print "Exists\n" 	if exists $hash{$key};
     print "Defined\n" 	if defined $hash{$key};
@@ -1416,7 +1419,7 @@
     print "Defined\n" 	if defined $array[$index];
     print "True\n"      if $array[$index];
 
-A hash element can be true only if it's defined, and defined if
+A hash or array element can be true only if it's defined, and defined if
 it exists, but the reverse doesn't necessarily hold true.
 
 Note that the EXPR can be arbitrarily complicated as long as the final
End of Patch.


References to:
Mark-Jason Dominus <mjd@plover.com>

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