Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Convert binary structure into ...Retrieve the next key/value pa ... (Berikutnya)
Functions for real %HASHes

Deletes a value from a hash

Daftar Isi

  • delete EXPR

    Given an expression that specifies an element or slice of a hash, deletedeletes the specified elements from that hash so that exists() on that elementno longer returns true. Setting a hash element to the undefined value doesnot remove its key, but deleting it does; see exists.

    In list context, returns the value or values deleted, or the last suchelement in scalar context. The return list's length always matches that ofthe argument list: deleting non-existent elements returns the undefined valuein their corresponding positions.

    delete() may also be used on arrays and array slices, but its behavior is lessstraightforward. Although exists() will return false for deleted entries,deleting array elements never changes indices of existing values; use shift()or splice() for that. However, if all deleted elements fall at the end of anarray, the array's size shrinks to the position of the highest element thatstill tests true for exists(), or to 0 if none do.

    WARNING: Calling delete on array values is deprecated and likely tobe removed in a future version of Perl.

    Deleting from %ENV modifies the environment. Deleting from a hash tied toa DBM file deletes the entry from the DBM file. Deleting from a tied hashor array may not necessarily return anything; it depends on the implementationof the tied package's DELETE method, which may do whatever it pleases.

    The delete local EXPR construct localizes the deletion to the currentblock at run time. Until the block exits, elements locally deletedtemporarily no longer exist. See Localized deletion of elements of composite types in perlsub.

    1. %hash = (foo => 11, bar => 22, baz => 33);
    2. $scalar = delete $hash{foo}; # $scalar is 11
    3. $scalar = delete @hash{qw(foo bar)}; # $scalar is 22
    4. @array = delete @hash{qw(foo bar baz)}; # @array is (undef,undef,33)

    The following (inefficiently) deletes all the values of %HASH and @ARRAY:

    1. foreach $key (keys %HASH) {
    2. delete $HASH{$key};
    3. }
    4. foreach $index (0 .. $#ARRAY) {
    5. delete $ARRAY[$index];
    6. }

    And so do these:

    1. delete @HASH{keys %HASH};
    2. delete @ARRAY[0 .. $#ARRAY];

    But both are slower than assigning the empty listor undefining %HASH or @ARRAY, which is the customary way to empty out an aggregate:

    1. %HASH = (); # completely empty %HASH
    2. undef %HASH; # forget %HASH ever existed
    3. @ARRAY = (); # completely empty @ARRAY
    4. undef @ARRAY; # forget @ARRAY ever existed

    The EXPR can be arbitrarily complicated provided itsfinal operation is an element or slice of an aggregate:

    1. delete $ref->[$x][$y]{$key};
    2. delete @{$ref->[$x][$y]}{$key1, $key2, @morekeys};
    3. delete $ref->[$x][$y][$index];
    4. delete @{$ref->[$x][$y]}[$index1, $index2, @moreindices];
 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Convert binary structure into ...Retrieve the next key/value pa ... (Berikutnya)